eslint/no-undefined Restriction
作用
禁止将 undefined 作为标识符使用。
为什么这不好?
直接使用 undefined 可能会导致 bug,因为在 JavaScript 中它可能被遮蔽或被覆盖。 更安全、更明确的做法是使用 null,或者依赖隐式的 undefined(例如,没有返回值)来避免意外问题。
示例
以下是此规则的错误代码示例:
javascript
var foo = undefined;
var undefined = "foo";
if (foo === undefined) {
// ...
}
function baz(undefined) {
// ...
}
bar(undefined, "lorem");以下是此规则的正确代码示例:
javascript
var foo = void 0;
var Undefined = "foo";
if (typeof foo === "undefined") {
// ...
}
global.undefined = "foo";
bar(void 0, "lorem");如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-undefined": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-undefined": "error",
},
});bash
oxlint --deny no-undefined版本
此规则于 v0.5.3 中添加。
