TypeScript不尊重get-only属性?

时间:2017-08-02 21:19:29

标签: typescript casting immutability assignment-operator

考虑以下代码

class A {
    private _f: string;
    get f(): string {
        return this._f;
    }
}
class B {
    f: string;    
    static x(a: A): B {
        // I expect an error in next line because A does not have a setter for 'f'
        return a;
    }
}
let a = new A();
// this line gives error, as expected
a.f = "safe";
let b = B.x(a);
// this will mutate object that I want to be immutable!
b.f = "ouch!";

为什么会这样? (尝试使用TS 2.4和2.3)。

1 个答案:

答案 0 :(得分:1)

经过一番浏览后发现它是known problem ...