Search code examples
typescripttsconfig

Typescript tsconfig not respecting noUnusedLocal rule


I have a project using typescript 3.6.3.

In my root I have a tsconfig.json with noUnusedLocals: true:

{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true,

  },
  "include": ["*"],
  "exclude": ["node_modules/*"]
}

I have a file in the same root directory containing const unusedVar = '';. When I run npx tsc --project tsconfig.json it does not throw an error during compilation.

If I add sourceMap: true to my tsconfig and compile, the source maps get added, so my config is being respected. But why does noUnusedLocals not work?


Solution

  • If you have a file with just

    const unusedVar = '';
    

    then it's expected the warning triggered: noUnusedLocals is a rule that triggers on unused local variables, while that variable you declared is global.

    To make it local put it in a function or simply in an unnamed block { const unusedVar = ''; }