为什么编译? (TS v2.0.3)
class SetterOnly {
set prop(v) {
let x = this.prop;
}
}
我期待 this.prop
生成编译时错误...
为什么编译? (TS v2.0.3)
class SetterOnly {
set prop(v) {
let x = this.prop;
}
}
我期待 this.prop
生成编译时错误...
这是一个已知的问题: https://github.com/Microsoft/TypeScript/issues/814
我们绝对不会为只写属性而烦恼。这并不足以证明使用类型系统复杂化是合理的。
TypeScript没有概念 writeonly
在这一刻。仅仅因为对它的需求不多。但确实如此 readonly
:
class ReadOnly {
get prop() {return 123}
}
const readonly = new ReadOnly();
readonly.prop = 123; // Error