应该默认"得到"访问器描述符(具有已定义的" set"访问器描述符)具有类型" undefined"?

时间:2017-08-14 23:48:20

标签: typescript

如果我们看一下这个例子:

class MyClass {                                                              

  private _myVar: string;                                                    

  public constructor() {                                                     
    this._myVar = 'Hello world!';                                            
  }                                                                          

  public set myVar(value: string) {                                          
    this._myVar = value;                                                     
  }                                                                          

}                                                                            

const myObj = new MyClass();                                                 

const otherVar: string = myObj.myVar;

没有产生错误,但应该有吗?

该类没有get属性的myVar键,因此它默认为undefined,但是TypeScript编译器并没有接受它并说最后一次otherVar任务没问题。

最后的分配错误不应该出来吗?

1 个答案:

答案 0 :(得分:0)