Skip to content

eslint/no-magic-numbers Style

🚧 An auto-fix is still under development.

What it does

This rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make their meaning explicit. The current implementation does not support BigInt numbers inside array indexes.

Why is this bad?

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

Examples

Examples of incorrect code for this rule:

javascript
var dutyFreePrice = 100;
var finalPrice = dutyFreePrice + (dutyFreePrice * 0.25);

Examples of correct code for this rule with option "ignore":

javascript
/*typescript no-magic-numbers: ["error", { "ignore": [1] }]*/
var data = ["foo", "bar", "baz"];
var dataLast = data.length && data[data.length - 1];

Examples of correct code for this rule with option "ignoreArrayIndexes":

javascript
/*typescript no-magic-numbers: ["error", { "ignoreArrayIndexes": true }]*/
var item = data[2];
data[100] = a;
f(data[0]);
a = data[-0]; // same as data[0], -0 will be coerced to "0"
a = data[0xAB];
a = data[5.6e1];
a = data[4294967294]; // max array index

Examples of correct code for this rule with option "ignoreDefaultValues":

javascript
/*typescript no-magic-numbers: ["error", { "ignoreDefaultValues": true }]*/
const { tax = 0.25 } = accountancy;
function mapParallel(concurrency = 3) {/***/}

Examples of correct code for this rule with option "ignoreClassFieldInitialValues":

javascript
/*typescript no-magic-numbers: ["error", { "ignoreClassFieldInitialValues": true }]*/
class C {
  foo = 2;
  bar = -3;
  #baz = 4;
  static qux = 5;
}

Examples of incorrect code for this rule with option "enforceConst":

javascript
/*typescript no-magic-numbers: ["error", { "enforceConst": true }]*/
var TAX = 0.25;

Examples of incorrect code for this rule with option "detectObjects":

javascript
/*typescript no-magic-numbers: ["error", { "detectObjects": true }]*/
var magic = {
  tax: 0.25,
};

Examples of correct code for this rule with option "detectObjects":

javascript
/*typescript no-magic-numbers: ["error", { "detectObjects": true }]*/
var TAX = 0.25;

var magic = {
  tax: TAX,
};

Examples of correct code for this rule with option "ignoreEnums":

typescript
/*typescript no-magic-numbers: ["error", { "ignoreEnums": true }]*/
enum foo {
  SECOND = 1000,
}

Examples of correct code for this rule with option "ignoreNumericLiteralTypes":

typescript
/*typescript no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/
type SmallPrimes = 2 | 3 | 5 | 7 | 11;

Examples of correct code for this rule with option "ignoreReadonlyClassProperties":

typescript
/*typescript no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": true }]*/
class Foo {
  readonly A = 1;
  readonly B = 2;
  public static readonly C = 1;
  static readonly D = 1;
}

Examples of correct code for this rule with option "ignoreTypeIndexes":

typescript
/*typescript no-magic-numbers: ["error", { "ignoreTypeIndexes": true }]*/
type Foo = Bar[0];
type Baz = Parameters<Foo>[2];

Configuration

This rule accepts a configuration object with the following properties:

detectObjects

type: boolean

default: false

When true, numeric literals used in object properties are considered magic numbers.

enforceConst

type: boolean

default: false

When true, enforces that number constants must be declared using const instead of let or var.

ignore

type: array

default: []

An array of numbers to ignore if used as magic numbers. Can include floats or BigInt strings.

ignore[n]

ignoreArrayIndexes

type: boolean

default: false

When true, numeric literals used as array indexes are ignored.

ignoreClassFieldInitialValues

type: boolean

default: false

When true, numeric literals used as initial values in class fields are ignored.

ignoreDefaultValues

type: boolean

default: false

When true, numeric literals used as default values in function parameters and destructuring are ignored.

ignoreEnums

type: boolean

default: false

When true, numeric literals in TypeScript enums are ignored.

ignoreNumericLiteralTypes

type: boolean

default: false

When true, numeric literals used as TypeScript numeric literal types are ignored.

ignoreReadonlyClassProperties

type: boolean

default: false

When true, numeric literals in readonly class properties are ignored.

ignoreTypeIndexes

type: boolean

default: false

When true, numeric literals used to index TypeScript types are ignored.

How to use

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

bash
oxlint --deny no-magic-numbers
json
{
  "rules": {
    "no-magic-numbers": "error"
  }
}

References

Released under the MIT License.