Skip to main content

no-magic-numbers

Disallow magic numbers.

🧱

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

❄️

This rule is currently frozen and is not accepting feature requests.

This rule extends the base no-magic-numbers rule from ESLint core. It adds support for:

  • numeric literal types (type T = 1),
  • enum members (enum Foo { bar = 1 }),
  • readonly class properties (class Foo { readonly bar = 1 }).

How to Use​

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

Try this rule in the playground ↗

Options​

See eslint/no-magic-numbers's 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​

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

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

enum foo {
SECOND = 1000,
}
Open in Playground

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

enum foo {
SECOND = 1000,
}
Open in Playground

ignoreNumericLiteralTypes​

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

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

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

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

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

ignoreReadonlyClassProperties​

Whether readonly class properties are considered okay.

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

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​

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

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

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

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​

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.

Resources​

Taken with ❤️ from ESLint core.