【发布时间】:2019-09-05 21:47:26
【问题描述】:
我有这种情况,当我继承一个类时,基类中的属性不会被识别为在扩展类中初始化。我不确定这是否是 typescript 或 tslint 的问题,我在谷歌上找不到任何东西(可能没有搜索到正确的东西)。
(属性)BaseClass.myProperty:IMyProperty |未定义的对象是 可能是“未定义”.ts(2532)
tsconfig.json
{
"compilerOptions": {
"strict": true
}
}
example.ts
interface IMyProperty{
myName: string;
}
class BaseClass {
readonly myProperty: IMyProperty | undefined;
constructor(options: IMyProperty){
this.myProperty = options
}
}
class ExtendedClass extends BaseClass{
constructor(options: IMyProperty){
super(options)
}
printMyName(){
console.log(this.myProperty.myName); // <-- Complains about this
}
}
const extendedClass = new ExtendedClass({myName: 'John Smith'});
extendedClass.printMyName();
【问题讨论】:
标签: node.js typescript