Skip to content

Variables rules

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.

Check which value must be assigned or returned. Preserve mutations that another closure observes.

count = count++ keeps the old value. Use count += 1 when an increment is intended.

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.

Rename the binding, or stop assigning to the built-in.

function parse(String) { return String.trim(); } reports declaration. Name the parameter text.

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.

Rename the declared binding and its references. Property names can keep their external API spelling.

Use packageName instead of declaring a variable named package.

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.

Move the number into a module-level constant whose name says what it measures.

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.

Option 1: The options of @typescript-eslint/no-magic-numbers, passed through unchanged. Default: {}.

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.

Remove the assignment, or assign the value that the code meant to store.

let mode = "fast"; if (ready) mode = "fast"; reports redundant at the second "fast".

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.

Return or throw the initializer directly. Keep variables that enforce a type or have another use.

const result = read(); return result; becomes return read();.