liveBook · Manning

Prefer-const rule

As explained in Essential TypeScript 5, Third Edition

The prefer-const rule requires a const declaration when a variable declared with let is never reassigned.

The prefer-const rule flags a let declaration when its value is never reassigned and recommends const instead. ESLint reports the issue while checking TypeScript files against the configured rules. section 6.3

Typical lint result

Running npx eslint . can report errors such as “'message' is never reassigned. Use 'const' instead” and identify them as prefer-const violations. The same output can flag printMessage and total.

Configure the rule according to the project’s priorities:

  • Keep prefer-const enabled to receive warnings about let declarations that should use const. section 6.3
  • Set "prefer-const": 0 in the rules section of .eslintrc to disable the rule globally. section 6.3.1
  • Disable rules selectively when a warning is useful generally but inappropriate for a particular statement; the supplied example applies this approach to no-debugger. section 6.3.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines the declaration preference, demonstrates diagnostics, and configures global disabling without exploring rule internals.
Examples
Uses lint output for printMessage, message, and total, plus a .eslintrc setting that disables prefer-const.
What you can do after
Operate/configure ESLint by interpreting prefer-const diagnostics or disabling the rule globally when its warnings are unwanted.
How it connects
Connects ESLint configuration with TypeScript, let, const, linting rules, and debugger exceptions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.