Skip to main content

no-magic-numbers

禁止魔法数字.

🧱

This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.

This rule extends the base no-magic-numbers rule from ESLint core. 它增加了对以下内容的支持:

¥It adds support for:

  • 数字字面量类型(type T = 1),

    ¥numeric literal types (type T = 1),

  • enum 成员(enum Foo { bar = 1 }),

    ¥enum members (enum Foo { bar = 1 }),

  • readonly 类属性(class Foo { readonly bar = 1 })。

    ¥readonly class properties (class Foo { readonly bar = 1 }).

如何使用

eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-magic-numbers": "off",
"@typescript-eslint/no-magic-numbers": "error"
}
});

在线运行试试这个规则 ↗

选项

See eslint/no-magic-numbers's options.

¥Options

该规则添加了以下选项:

¥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

¥Whether enums used in TypeScript are considered okay. false by default.

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

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

enum foo {
SECOND = 1000,
}
Open in Playground

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

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

enum foo {
SECOND = 1000,
}
Open in Playground

ignoreNumericLiteralTypes

TypeScript 数字字面量类型中使用的数字是否被认为是可以的。默认情况下为 false

¥Whether numbers used in TypeScript numeric literal types are considered okay. false by default.

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

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

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

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

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

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

ignoreReadonlyClassProperties

readonly 类属性是否可行。

¥Whether readonly class properties are considered okay.

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

¥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 } 选项的正确代码示例:

¥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

¥Whether numbers used to index types are okay. false by default.

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

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

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

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

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

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

何时不使用它

¥When Not To Use It

如果你的项目经常处理常量,并且你不希望占用额外的空间来声明它们,则此规则可能不适合你。我们建议至少使用描述性注释和/或名称来描述常量。你可以考虑使用 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.

资源

Taken with ❤️ from ESLint core.