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

1

12

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.

Frightfully answered 3/4, 2019 at 4:54 Comment(3)
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. If you have a variable which is never read from the variable is useless, just remove it.Conceptionconceptual
Why keep the state if you're not doing anything with the state? The moment you do something with the tick variable, typescript will stop complaining.Shortening
Oh... Right.. it's late and I'm reading lint errors vs reading the code. I should have caught that. Like you said once I'm doing something all is good...Frightfully
C
24

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

Take a look at the compiler options for more info.

Crazyweed answered 3/4, 2019 at 4:57 Comment(3)
I personally don't like ignoring this error after learning more over the year it's a good idea to just remove the bad code vs leaving it.Frightfully
I can understand when this gets in the way during some experiments, such as when writing tests. And you might well be aware of the violation but don't want the hassle of commenting in/out temporarily unused vars/params. You just want the code to run. A lodal tslint.json usually helps but not for TS6133 from what I can see.Martines
Still there, even after changing it to false.Intervention

© 2022 - 2024 — McMap. All rights reserved.