1
export class LinkedList<t>
我目前使用创建一个超类:
export class ListBody extends collections.LinkedList<Element>
而这一切的伟大工程。但是,我在其他几个类中声明了这个类,并且在每个地方它实际上只需要几个Element的超类。所以我想在茨艾伦其他地方,而不是声明:
this.body = new moduleListBody.ListBody();
声明:
this.body = new moduleListBody.ListBody<ElementOne | ElementTwo>();
有没有办法做到这一点?我不能跳过ListBody类,因为我有很多代码在Element对象上运行。
更新:例如
export class Element {
}
export class LinkedList<t> {
public add(index : number, value : t);
public add(index : number, collection : LinkedList<t>);
public add(index : number, collection : t[]);
add(index : number, arg : t | LinkedList<t> | t[]) {
}
}
export class ListBody <T extends Element> extends LinkedList<T> {
public fubar(): void {
var element = new Element();
this.add(0, element);
}
}
非常感谢你! – 2015-03-27 14:42:42
这不起作用。当我调用LinkedList上的方法传递Element对象时,我收到了一个编译错误 - 元素不能分配给类型为T的参数。 – 2015-03-27 16:10:51
有趣,你可以发布[gist](https://gist.github.com/ )与相关的代码。我可以尝试帮助你! – wjohnsto 2015-03-27 16:13:34