close

no-magic-numbers

Unreleased

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-magic-numbers': 'error',
    },
  },
]);

Rule Details

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

The no-magic-numbers rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make their meaning explicit.

Examples of incorrect code for this rule:

const dutyFreePrice = 100,
  finalPrice = dutyFreePrice + dutyFreePrice * 0.25;

const data = ['foo', 'bar', 'baz'];
const dataLast = data[2];

let SECONDS;
SECONDS = 60;

Examples of correct code for this rule:

const TAX = 0.25;

const dutyFreePrice = 100,
  finalPrice = dutyFreePrice + dutyFreePrice * TAX;

Options

This rule accepts an object with the following properties:

  • ignore (default []): an array of numbers to ignore. Values may be number or a string parsed as a bigint literal (e.g. "100n").
  • ignoreArrayIndexes (default false): whether numbers used as array indexes are considered okay.
  • ignoreDefaultValues (default false): whether numbers used in default value assignments are considered okay.
  • ignoreClassFieldInitialValues (default false): whether numbers used as initial values of class fields are considered okay.
  • enforceConst (default false): whether to check for the const keyword in variable declarations of numbers.
  • detectObjects (default false): whether to detect numbers when setting object properties.
  • ignoreEnums (default false, TypeScript only): whether numbers used in enum members are considered okay.
  • ignoreNumericLiteralTypes (default false, TypeScript only): whether numbers used in numeric literal types are considered okay.
  • ignoreReadonlyClassProperties (default false, TypeScript only): whether numbers used in readonly class properties are considered okay.
  • ignoreTypeIndexes (default false, TypeScript only): whether numbers used to index types are considered okay.

ignore

Examples of correct code for this rule with { "ignore": [1] }:

{ "no-magic-numbers": ["error", { "ignore": [1] }] }
const data = ['foo', 'bar', 'baz'];
const dataLast = data.length && data[data.length - 1];

Examples of correct code for this rule with { "ignore": ["1n"] }:

{ "no-magic-numbers": ["error", { "ignore": ["1n"] }] }
foo(1n);

ignoreArrayIndexes

This option allows only valid array indexes: numbers that will be coerced to one of "0", "1", "2" ... "4294967294".

Examples of correct code for this rule with { "ignoreArrayIndexes": true }:

{ "no-magic-numbers": ["error", { "ignoreArrayIndexes": true }] }
const item = data[2];
data[100] = a;
f(data[0]);
a = data[-0]; // same as data[0], -0 will be coerced to "0"
a = data[10n]; // same as data[10], 10n will be coerced to "10"
a = data[4294967294]; // max array index

Examples of incorrect code for this rule with { "ignoreArrayIndexes": true }:

{ "no-magic-numbers": ["error", { "ignoreArrayIndexes": true }] }
f(2); // not used as array index
a = data[-1];
a = data[2.5];
a = data[4294967295]; // above the max array index

ignoreDefaultValues

Examples of correct code for this rule with { "ignoreDefaultValues": true }:

{ "no-magic-numbers": ["error", { "ignoreDefaultValues": true }] }
const { tax = 0.25 } = accountancy;

function mapParallel(concurrency = 3) {}

ignoreClassFieldInitialValues

Examples of correct code for this rule with { "ignoreClassFieldInitialValues": true }:

{ "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 { "ignoreClassFieldInitialValues": true }:

{ "no-magic-numbers": ["error", { "ignoreClassFieldInitialValues": true }] }
class C {
  foo = 2 + 3;
}

enforceConst

Examples of incorrect code for this rule with { "enforceConst": true }:

{ "no-magic-numbers": ["error", { "enforceConst": true }] }
let TAX = 0.25;

detectObjects

Examples of incorrect code for this rule with { "detectObjects": true }:

{ "no-magic-numbers": ["error", { "detectObjects": true }] }
const magic = {
  tax: 0.25,
};

ignoreEnums

Examples of correct TypeScript code for this rule with { "ignoreEnums": true }:

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

ignoreNumericLiteralTypes

Examples of correct TypeScript code for this rule with { "ignoreNumericLiteralTypes": true }:

{ "no-magic-numbers": ["error", { "ignoreNumericLiteralTypes": true }] }
type Foo = 1 | 2 | 3;

ignoreReadonlyClassProperties

Examples of correct TypeScript code for this rule with { "ignoreReadonlyClassProperties": true }:

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

ignoreTypeIndexes

Examples of correct TypeScript code for this rule with { "ignoreTypeIndexes": true }:

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

Original Documentation