close

no-magic-numbers

Configuration

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

export default defineConfig([
  ts.configs.recommended,
  {
    rules: {
      '@typescript-eslint/no-magic-numbers': 'error',
    },
  },
]);

Rule Details

Disallow magic numbers. A "magic number" is a numeric literal that is used in the code without explanation or assignment to a named constant. Magic numbers make code less readable and harder to maintain.

Examples of incorrect code for this rule:

var total = 500;
if (foo === 10) {}
var data = ['foo', 'bar', 'baz'];
var dataLast = data[2];

Examples of correct code for this rule:

var TAX = 0.25;
var total = 500;
if (foo === TAX) {}
const data = ['foo', 'bar', 'baz'];
const LAST = 2;
var dataLast = data[LAST];

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

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

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

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

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

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

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

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

Original Documentation

ESLint - no-magic-numbers typescript-eslint - no-magic-numbers