TypeScript error: foo is declared but its value is never read. TS6133

JustDave picture JustDave · Apr 3, 2019 · Viewed 12.6k times · Source

Something simple that will cause this to happen.

let _tick = 0;
this.app.ticker.add( () => {
   moveSprites(dots);
   _tick += .2;
   return;
});

Tslint options are set to the following:

"rules": {
  "object-literal-sort-keys": false,
  "no-unused-variable": [true, {"ignore-pattern": "^_"}]
}

From searching I thought that rule would resolve and allow it to be ignored but nope.

One solution was to write it like this. It will pass but it will then complain about the tick += .2 being assigned but never used. Plus, this changes the behavior.

this.app.ticker.add( (tick = 0) => {
   moveSprites(dots);
   tick += .2;
   return;
});

Then finally I found // @ts-ignore and that worked... I'm new to typescript and I could see this being a problem in instances were you just need to keep up a variables state; only ever setting it. I also see some conventions to _var name as protected class fields but also for these instances as well? What is the right way? I like the benefits of TS, but being new I'm spending a lot of time appeasing the ts linter.

Answer

lleon picture lleon · Apr 3, 2019

You can disable the check setting noUnusedLocals to false in your tsconfig.json file.

Take a look at the compiler options for more info.