如果我们看一下这个例子:
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
任务没问题。
最后的分配错误不应该出来吗?