Variables rules
no-discarded-postfix
Section titled “no-discarded-postfix”Reports postfix updates assigned back to the same variable or returned from the declaring scope.
On in the recommended and all configs.
A postfix expression returns the old value, so an assignment or return can discard the intended update.
What to do
Section titled “What to do”Check which value must be assigned or returned. Preserve mutations that another closure observes.
Example
Section titled “Example”count = count++ keeps the old value. Use count += 1 when an increment is intended.
no-globals-shadowing
Section titled “no-globals-shadowing”Reports a variable, function, parameter, or catch binding named after a built-in global of JavaScript, such as Object or parseInt, and an assignment or update of one of these names.
On in the recommended and all configs.
A local name that hides a built-in makes the built-in unreachable in that scope, and readers expect the standard behavior.
What to do
Section titled “What to do”Rename the binding, or stop assigning to the built-in.
Example
Section titled “Example”function parse(String) { return String.trim(); } reports declaration. Name the parameter text.
no-keyword-declarations
Section titled “no-keyword-declarations”Reports declared bindings that use JavaScript reserved or future reserved words.
On in the recommended and all configs.
Reserved binding names can stop parsing when code changes language mode or moves between tools.
What to do
Section titled “What to do”Rename the declared binding and its references. Property names can keep their external API spelling.
Example
Section titled “Example”Use packageName instead of declaring a variable named package.
no-magic-numbers
Section titled “no-magic-numbers”Reports the numbers that @typescript-eslint/no-magic-numbers reports, except in the value of a module-level const, a split limit, or a size that a destructuring pattern names. It takes the options of that rule and needs it installed.
No config turns it on; turn it on in your ESLint config.
A bare number hides what it measures, and a module-level constant gives it a name that callers share.
What to do
Section titled “What to do”Move the number into a module-level constant whose name says what it measures.
Example
Section titled “Example”The call setTimeout(retry, 3000) reports noMagic. Name the delay, such as const RETRY_DELAY_MS = 3000; at module level, and pass the constant. const SIZES = [16, 32, 64]; and const TIMEOUT_MS = 60 * 1000; pass, because the constant names its numbers. export const server = app.listen(8080); reports noMagic, because server names the server, not the port. const [key, value] = line.split("=", 2); passes, because the two names give the split limit its meaning.
Options
Section titled “Options”Option 1: The options of @typescript-eslint/no-magic-numbers, passed through unchanged. Default: {}.
no-redundant-assignment
Section titled “no-redundant-assignment”Reports an assignment of a literal to a variable that already holds that same literal on every code path that reaches the assignment.
On in the recommended and all configs.
An assignment that cannot change the value suggests that the code meant to assign something else, and it hides which value the code relies on.
What to do
Section titled “What to do”Remove the assignment, or assign the value that the code meant to store.
Example
Section titled “Example”let mode = "fast"; if (ready) mode = "fast"; reports redundant at the second "fast".
prefer-immediate-return
Section titled “prefer-immediate-return”Reports a variable created only to return or throw its value on the next statement.
On in the all config.
An intermediate variable adds a name without adding a step to the computation.
What to do
Section titled “What to do”Return or throw the initializer directly. Keep variables that enforce a type or have another use.
Example
Section titled “Example”const result = read(); return result; becomes return read();.