no-magic-numbers
UnreleasedConfiguration
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:
Examples of correct code for this rule:
Options
This rule accepts an object with the following properties:
ignore(default[]): an array of numbers to ignore. Values may benumberor astringparsed as abigintliteral (e.g."100n").ignoreArrayIndexes(defaultfalse): whether numbers used as array indexes are considered okay.ignoreDefaultValues(defaultfalse): whether numbers used in default value assignments are considered okay.ignoreClassFieldInitialValues(defaultfalse): whether numbers used as initial values of class fields are considered okay.enforceConst(defaultfalse): whether to check for theconstkeyword in variable declarations of numbers.detectObjects(defaultfalse): whether to detect numbers when setting object properties.ignoreEnums(defaultfalse, TypeScript only): whether numbers used in enum members are considered okay.ignoreNumericLiteralTypes(defaultfalse, TypeScript only): whether numbers used in numeric literal types are considered okay.ignoreReadonlyClassProperties(defaultfalse, TypeScript only): whether numbers used inreadonlyclass properties are considered okay.ignoreTypeIndexes(defaultfalse, TypeScript only): whether numbers used to index types are considered okay.
ignore
Examples of correct code for this rule with { "ignore": [1] }:
Examples of correct code for this rule with { "ignore": ["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 }:
Examples of incorrect code for this rule with { "ignoreArrayIndexes": true }:
ignoreDefaultValues
Examples of correct code for this rule with { "ignoreDefaultValues": true }:
ignoreClassFieldInitialValues
Examples of correct code for this rule with { "ignoreClassFieldInitialValues": true }:
Examples of incorrect code for this rule with { "ignoreClassFieldInitialValues": true }:
enforceConst
Examples of incorrect code for this rule with { "enforceConst": true }:
detectObjects
Examples of incorrect code for this rule with { "detectObjects": true }:
ignoreEnums
Examples of correct TypeScript code for this rule with { "ignoreEnums": true }:
ignoreNumericLiteralTypes
Examples of correct TypeScript code for this rule with { "ignoreNumericLiteralTypes": true }:
ignoreReadonlyClassProperties
Examples of correct TypeScript code for this rule with { "ignoreReadonlyClassProperties": true }:
ignoreTypeIndexes
Examples of correct TypeScript code for this rule with { "ignoreTypeIndexes": true }: