Skip to content

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

Closed

Description

@vczh

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

Activity

  1. ajafff commented on Nov 17, 2019

    @ajafff
    Contributor

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

  2. dragomirtitian commented on Nov 18, 2019

    @dragomirtitian
    Contributor

    Klaus Meinhardt (@ajafff) It does come form the compiler if noUnusedLocals is used.

    Playground Link

  3. ajafff commented on Nov 18, 2019

    @ajafff
    Contributor

    Titian Cernicova-Dragomir (@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.

  4. RyanCavanaugh commented on Nov 18, 2019

    @RyanCavanaugh
    Member

    One of these errors comes from tslint

  5. vczh commented on Nov 18, 2019

    @vczh
    Author

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

  6. ajafff commented on Nov 19, 2019

    @ajafff
    Contributor

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

  7. vczh commented on Nov 19, 2019

    @vczh
    Author

    Klaus Meinhardt (@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.

  8. 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)'.[/+] on Nov 19, 2019
  9. dragomirtitian commented on Nov 19, 2019

    @dragomirtitian
    Contributor

    vczh Yes but as Klaus Meinhardt (@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions