Skip to main content

no-magic-numbers

Disallow magic numbers.


此规则扩展了基本 eslint/no-magic-numbers 规则。 它增加了对以下内容的支持:

英:This rule extends the base eslint/no-magic-numbers rule. It adds support for:

  • 数字字面量类型 (type T = 1),
  • enum 名成员(enum Foo { bar = 1 } 名),
  • readonly 类属性 (class Foo { readonly bar = 1 })。

选项

该规则添加了以下选项:

英:This rule adds the following options:

interface Options extends BaseNoMagicNumbersOptions {
ignoreEnums?: boolean;
ignoreNumericLiteralTypes?: boolean;
ignoreReadonlyClassProperties?: boolean;
ignoreTypeIndexes?: boolean;
}

const defaultOptions: Options = {
...baseNoMagicNumbersDefaultOptions,
ignoreEnums: false,
ignoreNumericLiteralTypes: false,
ignoreReadonlyClassProperties: false,
ignoreTypeIndexes: false,
};

ignoreEnums

一个布尔值,用于指定 TypeScript 中使用的枚举是否可以接受。 默认为 false

英:A boolean to specify if enums used in TypeScript are considered okay. false by default.

{ "ignoreEnums": false } 选项的 incorrect 代码示例:

英:Examples of incorrect code for the { "ignoreEnums": false } option:

enum foo {
SECOND = 1000,
}
Open in Playground

{ "ignoreEnums": true } 选项的 correct 代码示例:

英:Examples of correct code for the { "ignoreEnums": true } option:

enum foo {
SECOND = 1000,
}
Open in Playground

ignoreNumericLiteralTypes

一个布尔值,用于指定 TypeScript 数字字面量类型中使用的数字是否被认为可以。 默认为 false

英:A boolean to specify if numbers used in TypeScript numeric literal types are considered okay. false by default.

{ "ignoreNumericLiteralTypes": false } 选项的 incorrect 代码示例:

英:Examples of incorrect code for the { "ignoreNumericLiteralTypes": false } option:

type SmallPrimes = 2 | 3 | 5 | 7 | 11;
Open in Playground

{ "ignoreNumericLiteralTypes": true } 选项的 correct 代码示例:

英:Examples of correct code for the { "ignoreNumericLiteralTypes": true } option:

type SmallPrimes = 2 | 3 | 5 | 7 | 11;
Open in Playground

ignoreReadonlyClassProperties

{ "ignoreReadonlyClassProperties": false } 选项的 incorrect 代码示例:

英:Examples of incorrect code for the { "ignoreReadonlyClassProperties": false } option:

class Foo {
readonly A = 1;
readonly B = 2;
public static readonly C = 1;
static readonly D = 1;
}
Open in Playground

{ "ignoreReadonlyClassProperties": true } 选项的 correct 代码示例:

英:Examples of correct code for the { "ignoreReadonlyClassProperties": true } option:

class Foo {
readonly A = 1;
readonly B = 2;
public static readonly C = 1;
static readonly D = 1;
}
Open in Playground

ignoreTypeIndexes

一个布尔值,用于指定用于索引类型的数字是否可以。 默认为 false

英:A boolean to specify if numbers used to index types are okay. false by default.

{ "ignoreTypeIndexes": false } 选项的 incorrect 代码示例:

英:Examples of incorrect code for the { "ignoreTypeIndexes": false } option:

type Foo = Bar[0];
type Baz = Parameters<Foo>[2];
Open in Playground

{ "ignoreTypeIndexes": true } 选项的 correct 代码示例:

英:Examples of correct code for the { "ignoreTypeIndexes": true } option:

type Foo = Bar[0];
type Baz = Parameters<Foo>[2];
Open in Playground

何时不使用它

如果你的项目经常处理常量,并且你不希望占用额外的空间来声明它们,则此规则可能不适合你。 我们建议至少使用描述性注释和/或名称来描述常量。 你可以考虑使用 ESLint 禁用注释 而不是完全禁用此规则。

英:If your project frequently deals with constant numbers and you don't wish to take up extra space to declare them, this rule might not be for you. We recommend at least using descriptive comments and/or names to describe constants. You might consider using ESLint disable comments instead of completely disabling this rule.

如何使用

.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-magic-numbers": "off",
"@typescript-eslint/no-magic-numbers": "error"
}
};
在线运行试试这个规则 ↗

选项

参见 eslint/no-magic-numbers 选项

资源

摘自 ❤️ ESLint 内核