Skip to content
← Back to rules

eslint/no-use-before-define Restriction

它的作用

禁止在变量定义之前使用它们。

为什么这不好?

在声明之前引用标识符会隐藏 bug,并且 使代码顺序相关,难以推理。

示例

此规则的错误代码示例:

ts
new A();
var A = class {};

此规则的正确代码示例:

ts
var A = class {};
new A();

配置

此规则接受一个包含以下属性的配置对象:

allowNamedExports

type: boolean

default: false

允许出现在声明之前的命名导出。

classes

type: boolean

default: true

检查类声明。

enums

type: boolean

default: true

检查枚举声明。

functions

type: boolean

default: true

检查函数声明。

ignoreTypeReferences

type: boolean

default: true

忽略仅作为类型引用的用法。

typedefs

type: boolean

default: true

检查类型别名、接口和类型参数。

variables

type: boolean

default: true

检查变量声明。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-use-before-define": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-use-before-define": "error",
  },
});
bash
oxlint --deny no-use-before-define

版本

此规则于 v1.49.0 中添加。

参考资料