Skip to content

Latest commit

 

History

History
68 lines (44 loc) · 2.11 KB

no-magic-numbers.md

File metadata and controls

68 lines (44 loc) · 2.11 KB

Disallow Magic Numbers (@typescript-eslint/no-magic-numbers)

'Magic numbers' are numbers that occur multiple times in code without an explicit meaning. They should preferably be replaced by named constants.

Rule Details

The @typescript-eslint/no-magic-numbers rule extends the no-magic-numbers rule from ESLint core, and adds support for handling Typescript specific code that would otherwise trigger the rule.

See the ESLint documentation for more details on the no-magic-numbers rule.

Rule Changes

{
    // note you must disable the base rule as it can report incorrect errors
    "no-magic-numbers": "off",
    "@typescript-eslint/no-magic-numbers": ["error", { "ignoreNumericLiteralTypes": true }]
}

In addition to the options supported by the no-magic-numbers rule in ESLint core, the rule adds the following options:

ignoreNumericLiteralTypes

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

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

/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": false }]*/

type SmallPrimes = 2 | 3 | 5 | 7 | 11;

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

/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/

type SmallPrimes = 2 | 3 | 5 | 7 | 11;

ignoreEnums

A boolean to specify if enums used in Typescript are considered okay. false by default.

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

/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnum": false }]*/

enum foo = {
    SECOND = 1000,
}

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

/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnum": true }]*/

enum foo = {
    SECOND = 1000,
}

Taken with ❤️ from ESLint core