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

Created on 3 Nov 2017  ·  25Comments  ·  Source: microsoft/TypeScript

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 --noUnusedLocals

Expected behavior: No erros

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

Working as Intended

Most helpful comment

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
  },

All 25 comments

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

@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.

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.

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."

@mentatxx can you provide a self-contained repro? I can't reproduce the problem

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,

The same issue with angulal specs - declaring a spy leads to the same error.

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

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

Getting false positives with Jest tests ("typescript": "^2.7.2") 'fetchDevices' is declared but its value is never read.

import {
  fetchDevices
} from "./api";

describe("fetchDevices", () => {
  describe("positive scenario", () => {
    it("fetches devices data", () => {
      fetchDevices("").then(data =>

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;
    });
  }

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
  },

var somemodule = require ('somemodule');
protected varname1 = somemodule ;

Then work normally with varname1.
This fixes the issue.

Getting the same error with TS 3:

[ts] 'componentWillMount' is declared but its value is never read.
class AWSApp extends Component<any> {

    constructor(props) {
        super(props);
    }

    private componentWillMount():void {
        window.addEventListener('beforeunload', this.handleWindowClose);
    }

    private componentWillUnMount() {
        window.removeEventListener('beforeunload', this.handleWindowClose);
    }

    public render() {}
}

@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.

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

@ilearnio the default behavior of the compiler is to emit files even in the presence of errors. Effectively, all errors are warnings.

You can also use @ts-ignore comments to silence particular warnings.

You can also just turn off noUnusedLocals if you're not finding it valuable; I believe there is a TSLint rule that will provide most of the same coverage.

@RyanCavanaugh Yeah, it seems that my issue is more related to Mocha that I'm using for testing, because for regular compilation with tsc it still compiles the files on error. I'm running Mocha like so mocha --require ts-node/register mytest.spec.ts -watch --watch-extensions ts, and on any compiler error, like unused variable, it just shows me that error with stack trace and doesn't launch tests until I fix it

Another case with private parameters listed in constructor

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) {}
}

The code is working just right and renderer is got from propGenerator but:

ERROR in src/app/some.service.ts(11,23): error TS6138: Property 'renderer' is declared but its value is never read.

Repo https://github.com/DzmVasileusky/angular-ts-lint-issue-1

@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 work for me, big sister, thx O(∩_∩)O

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

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) {}
}

import React from 'react';
import ReactDom from 'react-dom';

class Demo extends Component{
render(){
Return


Wellcome hamxa


}

}
export default Demo;

Declaration or statement expected.ts(1128)

Was this page helpful?
0 / 5 - 0 ratings