Cannot assign to ... because it is a read-only property when using type guard in ctor
#37,823 opened on Apr 7, 2020
Description
A common pattern is declaring narrower types for members in derived classes. However, when combined with type guards, assignement to readonly members fails with an unexpected "Cannot assign to ... because it is a read-only property" error.
TypeScript Version: 3.7.2
Search Terms: class member read-only property type narrowing covariant type guard
Code
class Foo {
public readonly x: string | number;
constructor() {
if (isBar(this))
// error TS2540: Cannot assign to 'x' because it is a read-only property.
this.x = 'string';
else
// no error here
this.x = 123;
}
}
class Bar extends Foo {
// Declare narrower type for member in derived class
public readonly x: string;
}
function isBar(foo: Foo): foo is Bar {
return foo instanceof Bar;
}
Expected behavior:
Assignment to this.x should be allowed with or without type guard in the constructor
Actual behavior:
error TS2540: Cannot assign to 'x' because it is a read-only property.
Playground Link:
Related Issues:
It is as if the type guard creates an invisible alias (cf https://github.com/microsoft/TypeScript/issues/14241), and asignment inside the type guard fails.