【发布时间】:2016-12-23 11:00:15
【问题描述】:
有没有办法将“成员变量”定义为“扩展对象”而不是静态类型(不使用接口)?
类似这样的伪代码:
class Foo {
bar -> extends Rectangle;
constructor(barInstance:IRectangle){
this.bar = barInstance;
this.bar.getArea(); //<-- is code completed because interface IRectangle
// no type error
this.bar.someCustomFunction = function() {
}
}
}
而不是
class Foo {
bar: IRectangle;
//or
bar:Rectangle;
}
这样我可以添加未在基类或接口上定义的属性而不会出现类型错误,而且还可以从基类获得代码完成。呵呵,懒惰严格打字?
【问题讨论】:
标签: javascript class inheritance typescript