Advertisement
Intermediate TypeScript, tsconfig & Compiler Options

What do 'noUnusedLocals' and 'noUnusedParameters' do, and how do you intentionally exempt a genuinely unused parameter?

Short answer

Both flags flag declared-but-never-used local variables or function parameters as compile errors, helping catch dead code and leftover debugging variables; to intentionally allow an unused parameter (like a required positional parameter you don't need), prefix its name with an…

Save TypeScript mock interview
Advertisement
AModel answer
Both flags flag declared-but-never-used local variables or function parameters as compile errors, helping catch dead code and leftover debugging variables; to intentionally allow an unused parameter (like a required positional parameter you don't need), prefix its name with an underscore, which the compiler specifically exempts from this check.
// tsconfig.json: { "noUnusedParameters": true }
function handler(event: Event, _index: number) {
  console.log(event); // '_index' is unused but exempted due to the underscore prefix
}
Real-world example
Catching leftover debugging variables or genuinely dead code left behind during refactoring, enforced automatically in CI.
Likely follow-ups
  • Does this underscore-prefix exemption apply to unused LOCAL VARIABLES too, or only function parameters?
Related topics