【发布时间】:2018-01-05 09:29:56
【问题描述】:
我想根据对象类型使用不同的通用方法。这是我的代码示例:
export interface IStorable {
Enable : boolean;
}
async GetItemStored<T extends IStorable>(toExecute : () => Promise<T>){//Some Code}
async GetItem<T>(toExecute : () => Promise<T>){//Some Code}
Main<T>(toExecute : () => Promise<T>){
if(this.IsStorable(toExecute)){
await this.GetItemStored(this.CastInStorable(toExecute));
} else {
await this.GetItem(toExecute);
}
}
IsStorable<T>(() => Promise<T>) :boolean {
// ???
}
CastInStorable<T, U extends IStorable >(() => Promise<T>) : () => Promise<U> {
// ???
}
你能帮我写两个函数 IsStorable 和 CastInStorable
提前致谢
【问题讨论】:
标签: typescript generics interface casting