Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
somzJosf opened this issue Nov 2, 2017 · 26 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@somzJosf
Copy link

somzJosf commented Nov 2, 2017

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.

@somzJosf somzJosf changed the title False positive TS6133 (declared but never used) report. False positive Error - TS6133 (declared but never used) report. Nov 2, 2017
@somzJosf somzJosf changed the title False positive Error - TS6133 (declared but never used) report. False positive Error - TS6133 error (declared but its value is never read) report. Nov 2, 2017
@ajafff
Copy link
Contributor

ajafff commented Nov 2, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Nov 2, 2017

please see https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#write-only-references-are-unused

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 2, 2017
@somzJosf
Copy link
Author

somzJosf commented Nov 2, 2017

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

@somzJosf somzJosf closed this as completed Nov 5, 2017
@matjaz
Copy link

matjaz commented Dec 4, 2017

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.

@mentatxx
Copy link

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

@RyanCavanaugh
Copy link
Member

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

@mariohmol
Copy link

mariohmol commented Jan 29, 2018

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,

@agnislav
Copy link

agnislav commented Feb 1, 2018

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

@tho-graf
Copy link

tho-graf commented Feb 7, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Feb 7, 2018

Duplicate of #21478, should be fixed in typescript@next today, and in [email protected] coming out in the next week or so.

@stereobooster
Copy link

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

@walllzzz
Copy link

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

@javahaxxor
Copy link

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

@arijoon
Copy link

arijoon commented Apr 18, 2018

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

@vp93
Copy link

vp93 commented Jun 19, 2018

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

Then work normally with varname1.
This fixes the issue.

@jadbox
Copy link

jadbox commented Aug 1, 2018

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

@RyanCavanaugh
Copy link
Member

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

@ilearnio
Copy link

ilearnio commented Aug 8, 2018

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

@RyanCavanaugh
Copy link
Member

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

@ilearnio
Copy link

ilearnio commented Aug 8, 2018

@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

@DzmVasileusky
Copy link

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

@BertieGo
Copy link

@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

@Vadorequest
Copy link

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

@chris31389
Copy link

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

@hamzakhang12
Copy link

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)

@mSaurabh
Copy link

mSaurabh commented Mar 2, 2021

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

Holy Moly!! This was a genius suggestion. Worked like a charm. Thanks

@microsoft microsoft locked as resolved and limited conversation to collaborators Mar 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests