Skip to content
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

Unused underscore variables not being ignored by lint rules #19614

Closed
chmeyers opened this issue Oct 31, 2017 · 10 comments
Closed

Unused underscore variables not being ignored by lint rules #19614

chmeyers opened this issue Oct 31, 2017 · 10 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@chmeyers
Copy link

TypeScript Version: 2.7.0-dev.20171031, 2.6.1 (Regression from 2.5.3)

Expected behavior:
Variables with names that begin with an underscore do not cause an error if not used.

Actual behavior:
components/panel/panel.ts(22,11): error TS6133: '_someVariable' is declared but its value is never read.

@chmeyers chmeyers changed the title Unused underscore variables note being ignored by lint rules Unused underscore variables not being ignored by lint rules Oct 31, 2017
@ghost
Copy link

ghost commented Oct 31, 2017

Underscores may be used to indicate unused parameters. A local variable such as const _ = 0; will still be flagged. Could you provide a complete example to show a regression?

@chmeyers
Copy link
Author

Originally noticed this on this variable:
https://github.com/yebrahim/datalab/blob/52ab35257d8c338ba10dfbf631fa756e603e37c7/sources/web/datalab/polymer/components/auth-panel/auth-panel.ts#L22

Let me see if I can make a self-contained repro.

@ghost
Copy link

ghost commented Oct 31, 2017

this._signedIn is written to but it doesn't appear to be read anywhere. This is newly detected in ts2.6 (#17752).

@chmeyers
Copy link
Author

Repro:
class TestClass {
private _shouldBeIgnored: boolean;

_someFunction() {
this._shouldBeIgnored = true;
}
}

tsc --noUnusedLocals test.ts
(Works in 2.5.3, fails on 2.6.1 (and latest nightly))

@chmeyers
Copy link
Author

In our particular case these variables are read by the html page.

@ghost
Copy link

ghost commented Oct 31, 2017

We don't support underscore ignores for private fields, only for parameters. Maybe you could use // @ts-ignore (#19139) or a dummy read?
Alternately, if the field is being accessed from the outside you might reconsider marking it private...

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 31, 2017
@nikklassen
Copy link

nikklassen commented Nov 1, 2017

My use case for this is array destructing. I have a function that returns a "tuple" (an array like [int, string]) and in some cases I would like to ignore one of the elements i.e. const [_foo, bar] = func() but keep the tuple-like semantics. const bar = func()[1] no longer communicates that this is a tuple.

@aluanhaddad
Copy link
Contributor

@nikklassen you can use a leading , as in

const [ , bar ] = func();

@petemcwilliams
Copy link

This warning

is written to but it doesn't appear to be read anywhere

Would be more useful than the one I was getting in version 2.6.1

is declared but its value is never read.

Because it is a new check I was searching for quite a while to figure this out, seeing the variable was being used (written to) in the ts.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
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

6 participants