Skip to content

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

Closed
@vczh

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

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