no-require-imports
Disallow invocation of
require()
.
与 require()
相比,更喜欢较新的 ES6 风格的导入。
英:Prefer the newer ES6-style imports over require()
.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-require-imports": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
const lib1 = require('lib1');
const { lib2 } = require('lib2');
import lib3 = require('lib3');
Open in Playgroundimport * as lib1 from 'lib1';
import { lib2 } from 'lib2';
import * as lib3 from 'lib3';
Open in Playground何时不使用它
如果你的项目经常使用较旧的 CommonJS require
,那么此规则可能不适用于你。
如果你的项目中只有一部分使用 require
,那么你可能会考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
英:If your project frequently uses older CommonJS require
s, then this rule might not be applicable to you.
If only a subset of your project uses require
s then you might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
相关
选项
该规则不可配置。