TypeScript: False positive Error - TS6133 error (declared but its value is never read) report.

tsc --version Version 2.6.1 , Also tried on 2.7.0-dev.20171102

class SomeClass {
	private manualPrice: number = 0;

	public someMethod() {
		this.manualPrice = 10; 
	}
}

and then compile file: tsc <file> --noUnusedLocals

Expected behavior: No erros

Actual behavior: error TS6133: ‘manualPriceTotal’ is declared but its value is never read.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (6 by maintainers)

Most upvoted comments

Since you never use the value of this property anywhere, I’d say the error is expected.

Set this in your ts.config, I absolutely hate the compiler telling me about unused variables during dev time, sure, for prod build it makes sense but during development it is one of the most annoying things and the main reason I avoid golang

  "compilerOptions": {
   ...
    "noUnusedLocals": false
  },

Using private vars in .vue files in template reported as unused. Changing to protected solved the issue. Typescript does not know the context of .vue and templates, so it makes a perfect sense to mark as unused. OTOH we don’t want readonly template variables to be publicly writable.

@mhegazy: Thanks for the doc. In my case, I have been using this in my template. But, even it was marked as private, I don’t know how the template was getting its value. Anyway, that property is intended to be public. I fixed it in my code. And the error is now gone. Thanks.

It causes a lot of problems in Angular I do

import { Component, OnInit } from '@angular/core';

export class AppComponent implements OnInit, OnDestroy {

And OnInit is reported as “is declared but its value is never read.”

Still getting the same error with typescript 2.7.2, any solution ?

#metoo , here’s one of the many errors I get, administration.component.ts[21, 11]: ‘title’ is declared but its value is never read.

@Component({
  moduleId: module.id,
  selector: 'administration',
  template: require('./administration.html')
})
export class AdministrationComponent implements OnInit, OnDestroy {
  private title: string | SafeHtml;

 
    // Listen to change title event
    this.onChangeTitleSubscription = this.administrationService.onChangeTitle.subscribe((title) => {
      this.title = title;
    });
  }

prefix the variable with an underscore 😃

in @DzmVasileusky’s example

import { Injectable, Renderer2 } from '@angular/core';

const propGenerator = function(text) {
  console.log(this.renderer);
  return text;
};

@Injectable()
export class SomeService {
  someProp: string = propGenerator.call(this, 'Hello');
  constructor(private _renderer: Renderer2) {}
}

There must be a way to set severity of unused locals to “warning”, instead of fatal error that prevents the script from being compiled. Or ideally, it should be a part of linter (tslint) rather then compiler.

Here is a real world example of how annoying it is right now. When I’m writing unit tests, I usually comment some parts of code that should not be triggered while I’m writing tests, because it will do some DB requests that I don’t want to be triggered. I may comment half of a file and then it starts yelling at me about unused imports and it won’t compile my tests until I comment all of the imports as well

Duplicate of https://github.com/Microsoft/TypeScript/issues/21478, should be fixed in typescript@next today, and in typescript@2.7.2 coming out in the next week or so.

import React from ‘react’; import ReactDom from ‘react-dom’;

class Demo extends Component{ render(){ Return <div>

Wellcome hamxa

</div>

}

} export default Demo;

Declaration or statement expected.ts(1128)

This can be disabled by setting noUnusedParameters to false in tsconfig.json.

@vp93 your “workaround” introduces new errors in the class, there isn’t a TS bug here, and declaring a property doesn’t really mark it as used.

@jadbox private indicates “not used outside this class”, but this is not the case in your application. componentWillMount is going to be called by something outside the class so it should be public.

Hi there, I found a minimal repro. But it only occours in the watch-mode. If you checkout the repo and run tsc --noEmit -w -p . you will get the error.

https://github.com/thomaskempel/tsc2.7.1-bug

Having same issue here, have a variable that is declared, populated on ngOnInit and is used on the template… but TS can’t see this using in the template… in my case i have it changed in tsconfig.conf like "noUnusedLocals": true,