Skip to content

Unexpectedly report error on correct code: 'field' is declared but its value is never read.(6133)'. #35153

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
vczh opened this issue Nov 17, 2019 · 8 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@vczh
Copy link

vczh commented Nov 17, 2019

TypeScript Version: 3.7.2

Search Terms: Put the error message but I don't recognize any similar issue by reading titles.

Code

export class A {
    private field: {} = {};

    public constructor() {
        // empty
    }

    public setField(value: {}): void {
        this.setFieldInternal('field', value);
    }

    private setFieldInternal(name: 'field', value: {}): void {
        this[name] = value;
    }
}

Expected behavior: No error is reported

Actual behavior: 'field' is declared but its value is never read.(6133).

Private member field could be assigned by assigning to this[name] while name has the type 'field' (or any string literal union that contains 'field'). So I believe this error message is incorrect.

If I add readonly to this field, this[name] = value; will report another error: Cannot assign to 'field' because it is a read-only property.

The compiler leaves me no space for writing code like this.

Playground Link: N/A

Related Issues: N/A

@ajafff
Copy link
Contributor

ajafff commented Nov 17, 2019

Are you sure this comes from the compiler? This looks more like a linter error to me.

@dragomirtitian
Copy link
Contributor

@ajafff It does come form the compiler if noUnusedLocals is used.

Playground Link

@ajafff
Copy link
Contributor

ajafff commented Nov 18, 2019

@dragomirtitian the OP mentions Private member variable 'field' is never reassigned; mark it as 'readonly'. and that's definitely not a compiler error.

The error you get with noUnusedLocals ('field' is declared but its value is never read.) is correct, because the property is only written and never read.

@RyanCavanaugh
Copy link
Member

One of these errors comes from tslint

@RyanCavanaugh RyanCavanaugh added the External Relates to another program, environment, or user action which we cannot control. label Nov 18, 2019
@vczh
Copy link
Author

vczh commented Nov 18, 2019

I don't think so, since https://www.typescriptlang.org/play/ gives me this error too. Unless it has tslint turned on.

@ajafff
Copy link
Contributor

ajafff commented Nov 19, 2019

@vczh Look at the error message. It's different in the TypeScript playground

@vczh
Copy link
Author

vczh commented Nov 19, 2019

@ajafff playground shows 'field' is declared but its value is never read.(6133), there is still an error. OK let me change the title of this issue.

@vczh vczh changed the title Unexpectedly report error on correct code: Private member variable 'field' is never reassigned; mark it as 'readonly'. Unexpectedly report error on correct code: 'field' is declared but its value is never read.(6133)'. Nov 19, 2019
@dragomirtitian
Copy link
Contributor

dragomirtitian commented Nov 19, 2019

@vczh Yes but as @ajafff pointed out, that error is actually correct in the context of noUnusedLocals. Add a read of the field and the error goes away as expected.

Playground Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

4 participants