【发布时间】:2012-11-29 10:05:51
【问题描述】:
我有一个基类,组件:
module abc {
export class Component {}
}
我还有各种扩展这个基类的类:
module cde {
export class Position extends abc.Component {}
}
现在,有一个几乎只包含组件的实体类:
module abc {
export class Entity {
add(component: Component) {}
}
}
为什么我不能这样做:
var entity = new abc.Entity().add(new cde.Position());
编译器抱怨 Argument types do not match parameters 即使我的 Position 类扩展了 Component 类,这是 add 方法所期望的类型...
改成
var entity = new abc.Entity().add(<abc.Component>new cde.Position());
满足编译器错误,但我不明白为什么我需要像那样显式向下转换..
在阅读了 Breeze 的回答后,我拿出了我的实际代码,剪掉了不相关的部分,然后将其粘贴到了操场上……低,看看代码确实有效。也许这是 IntelliJ 的打字稿实现的问题?我会继续调查的。
【问题讨论】:
标签: typescript