【发布时间】:2017-03-16 00:25:10
【问题描述】:
此代码无法编译,因为调用 CreateItem 的第一个参数不是范围内的变量。
abstract class Catalog<ItemType, IRaw> {
private CreateItem<ItemType, IRaw>(c: new (raw: IRaw) => ItemType, raw: IRaw): ItemType {
return new c(raw);
}
public ServiceUrl: string;
public Items: KnockoutObservableArray<ItemType> = ko.observableArray<ItemType>();
public LoadState: KnockoutObservable<LoadState> = ko.observable<LoadState>(LoadState.NotStarted);
private loadChunk(): void {
var that = this;
if (that.LoadState() === LoadState.NotStarted)
that.LoadState(LoadState.Loading);
if (that.LoadState() === LoadState.Loading) {
$.get(this.ServiceUrl, that.getChunkParameters()).then((result: Array<IRaw>) => {
if (result.length === 0) {
that.LoadState(LoadState.Complete)
} else {
for (var raw of result){
let foo = this.CreateItem(ItemType, raw);
that.Items.push(foo);
}
}
});
}
}
如何获取对泛型参数 ItemType 值的变量引用,以便将其传递给 CreateInstance?
【问题讨论】:
标签: generics typescript