TypeScript 不遵守 get-only 属性?

标签 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)。

最佳答案

经过一番浏览,原来是一个known problem ...

关于TypeScript 不遵守 get-only 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45471183/

相关文章:

Angular 模板 : Disable div in production mode

angular - 在 Ionic 中编写 JSON 文件

C++ 自动类型转换 : wrong behaviour for a container class

c++ - 从 GetProcAddress 转换的函数的签名是否必须完全匹配?

java - 如何避免制作 ByteBuffer 的防御性副本?

immutability - jQuery 对象是不可变的?

javascript - 如何在for循环中循环 promise 并在满足条件时中断

html - 如何在 typescript 中打破 ngFor 循环

c++ - 如何修复 : error: invalid conversion from 'const MyClass*' to 'MyClass*'

python - 关于内存地址的变量赋值和修改