Closed
Description
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