问题 在TypeScript中,为什么访问(get)属性只有一个setter才是错误?


为什么编译? (TS v2.0.3)

class SetterOnly {
  set prop(v) {
    let x = this.prop;
  }
}

我期待 this.prop 生成编译时错误...


2313
2017-10-12 01:09


起源



答案:


这是一个已知的问题: https://github.com/Microsoft/TypeScript/issues/814

我们绝对不会为只写属性而烦恼。这并不足以证明使用类型系统复杂化是合理的。


7
2017-10-12 04:08





TypeScript没有概念 writeonly 在这一刻。仅仅因为对它的需求不多。但确实如此 readonly

class ReadOnly {
  get prop() {return 123}
}

const readonly = new ReadOnly();
readonly.prop = 123; // Error 

6
2017-10-12 04:05