TypeScript不尊重只能获取属性?
问题描述:
考虑下面的代码TypeScript不尊重只能获取属性?
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)。