no-var-requires
Disallow
require
statements except in import statements.
✅
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended"
可启用此规则。
也就是说,禁止使用 var foo = require("foo")
等形式。 请改用 ES6 样式导入或 import foo = require("foo")
导入。
英:In other words, the use of forms such as var foo = require("foo")
are banned. Instead use ES6 style imports or import foo = require("foo")
imports.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-var-requires": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
var foo = require('foo');
const foo = require('foo');
let foo = require('foo');
Open in Playgroundimport foo = require('foo');
require('foo');
import foo from 'foo';
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.
相关
选项
该规则不可配置。