TypeScript Reference ยท tsconfig: Type Checking Options
TypeScript noUnusedLocals option
Reference for noUnusedLocals: error TS6133 for unused variables, imports, functions and private members, with underscore rules and how it differs from lint.
noUnusedLocals reports an error for any local declaration that is never read: variables, functions, classes, type aliases, imports and private class members. Dead declarations clutter code and often point to a half-finished change, such as an import that is no longer needed after a refactor.
The check applies to code you can see in the same file. It does not apply to exported names, because other files may use them. It is not part of strict, and many projects enable it only in CI or leave the job to a linter.
import { readFile } from "node:fs/promises"; // error if never used const unused = 5; // error: declared but never read
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "noUnusedLocals": true } }
| Part | Description |
|---|---|
| Value | true or false. Default: false. Not included in strict. |
| Error | TS6133: 'x' is declared but its value is never read. Unused imports use TS6133 or TS6192 (all imports in a declaration unused). |
| Reported | Local const, let, var, functions, classes, type aliases, interfaces, enums, namespaces, imports and private properties. |
| Not reported | Exported declarations, parameters (see noUnusedParameters), and names starting with _ in destructuring and for...of. |
Usage notes
- Writing is not reading. A variable that is only assigned (
x = 5) is still reported, even if the assignment happens in a loop. - Imports used only as types count as used. With
verbatimModuleSyntax, writeimport typefor them. - Private members: a
privatefield or method never used inside its class is reported;#privatenames too. - Editor behavior: even when the option is off, editors gray out unused code as a suggestion. Turning the option on turns the hint into a real error.
- Generated code and tests. Scratch files often trip this check; use a separate
tsconfigfor them rather than turning the option off globally. - Difference from lint. The compiler check is type-aware but has no autofix in
tsc; editors offer "Remove unused declaration" quick fixes.