UNPKG

3.74 kBMarkdownView Raw
1# `no-magic-numbers`
2
3Disallow magic numbers.
4
5## Rule Details
6
7This rule extends the base [`eslint/no-magic-numbers`](https://eslint.org/docs/rules/no-magic-numbers) rule.
8It adds support for:
9
10- numeric literal types (`type T = 1`),
11- `enum` members (`enum Foo { bar = 1 }`),
12- `readonly` class properties (`class Foo { readonly bar = 1 }`).
13
14## How to Use
15
16```jsonc
17{
18 // note you must disable the base rule as it can report incorrect errors
19 "no-magic-numbers": "off",
20 "@typescript-eslint/no-magic-numbers": [
21 "error",
22 {
23 /* options */
24 }
25 ]
26}
27```
28
29## Options
30
31See [`eslint/no-magic-numbers` options](https://eslint.org/docs/rules/no-magic-numbers#options).
32This rule adds the following options:
33
34```ts
35interface Options extends BaseNoMagicNumbersOptions {
36 ignoreEnums?: boolean;
37 ignoreNumericLiteralTypes?: boolean;
38 ignoreReadonlyClassProperties?: boolean;
39 ignoreTypeIndexes?: boolean;
40}
41
42const defaultOptions: Options = {
43 ...baseNoMagicNumbersDefaultOptions,
44 ignoreEnums: false,
45 ignoreNumericLiteralTypes: false,
46 ignoreReadonlyClassProperties: false,
47 ignoreTypeIndexes: false,
48};
49```
50
51### `ignoreEnums`
52
53A boolean to specify if enums used in TypeScript are considered okay. `false` by default.
54
55Examples of **incorrect** code for the `{ "ignoreEnums": false }` option:
56
57```ts
58/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnums": false }]*/
59
60enum foo {
61 SECOND = 1000,
62}
63```
64
65Examples of **correct** code for the `{ "ignoreEnums": true }` option:
66
67```ts
68/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreEnums": true }]*/
69
70enum foo {
71 SECOND = 1000,
72}
73```
74
75### `ignoreNumericLiteralTypes`
76
77A boolean to specify if numbers used in TypeScript numeric literal types are considered okay. `false` by default.
78
79Examples of **incorrect** code for the `{ "ignoreNumericLiteralTypes": false }` option:
80
81```ts
82/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": false }]*/
83
84type SmallPrimes = 2 | 3 | 5 | 7 | 11;
85```
86
87Examples of **correct** code for the `{ "ignoreNumericLiteralTypes": true }` option:
88
89```ts
90/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreNumericLiteralTypes": true }]*/
91
92type SmallPrimes = 2 | 3 | 5 | 7 | 11;
93```
94
95### `ignoreReadonlyClassProperties`
96
97Examples of **incorrect** code for the `{ "ignoreReadonlyClassProperties": false }` option:
98
99```ts
100/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": false }]*/
101
102class Foo {
103 readonly A = 1;
104 readonly B = 2;
105 public static readonly C = 1;
106 static readonly D = 1;
107}
108```
109
110Examples of **correct** code for the `{ "ignoreReadonlyClassProperties": true }` option:
111
112```ts
113/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreReadonlyClassProperties": true }]*/
114
115class Foo {
116 readonly A = 1;
117 readonly B = 2;
118 public static readonly C = 1;
119 static readonly D = 1;
120}
121```
122
123### `ignoreTypeIndexes`
124
125A boolean to specify if numbers used to index types are okay. `false` by default.
126
127Examples of **incorrect** code for the `{ "ignoreTypeIndexes": false }` option:
128
129```ts
130/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreTypeIndexes": false }]*/
131
132type Foo = Bar[0];
133type Baz = Parameters<Foo>[2];
134```
135
136Examples of **correct** code for the `{ "ignoreTypeIndexes": true }` option:
137
138```ts
139/*eslint @typescript-eslint/no-magic-numbers: ["error", { "ignoreTypeIndexes": true }]*/
140
141type Foo = Bar[0];
142type Baz = Parameters<Foo>[2];
143```
144
145<sup>
146
147Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/main/docs/rules/no-magic-numbers.md)
148
149</sup>
150
151## Attributes
152
153- Configs:
154 - [ ] ✅ Recommended
155 - [ ] 🔒 Strict
156- [ ] 🔧 Fixable
157- [ ] 💭 Requires type information