Microsoft/TypeScript

Cannot assign to ... because it is a read-only property when using type guard in ctor

開放

#37,823 建立於 2020年4月7日

 (4 則留言) (2 個反應) (0 位負責人)TypeScript (13,395 個分叉)batch import
BugDomain: classesHelp Wanted

倉庫指標

星標
 (108,860 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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:

https://www.typescriptlang.org/play/?ssl=2&ssc=1&pln=22&pc=1#code/FAYwNghgzlAEBiB7RsDexadgBwK4CMwBLEWAJwFMIATRAOzAE9YAPALligBcyi6BzWAB9YdXAFt8FMgG5gGLCHrcyuEF0RkAFAEo0CrFiIAzWFqJQAQhG1cAFhZ06Dh1wHo3saWU2wAKgDKAEwArAAsAAwcAMIQdHSIXLDQUET8dLAasADkLNmwUiAQuFAUsERJFsnkVNQAtPRMOD7Y0lyMAHQurlj2Fh0ssAC8sACMQQDMcj2YFGCl3TMeoijevnbSFIs9fVADw2OT01gAvsBnoJAwsNZkXixcFHTUcEgo6IZ4hCQ1NI3M7E4PD4-DkF2MuDo6iI9HKVhsWmMyA4bx0HCRKCqt30hkoXFwZAyGPKdG4cRAFEQpluYOAQA

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.

貢獻者指南