我们仍然使用angular 1.5.8,并尝试编写一个函数,将长计算分解为零件。由于承诺可以返回诺言功能链接,我还以为写它这样的:在TypeScript中,如何定义一个返回承诺的递归函数
interface IResult {
add(s: string): void { ... }
}
function buildResult(): IResult { ... }
function handleItem(s: string): string { ... }
function doWork(data: string[]): ng.IPromise<IResult> {
let i = 0;
const result = buildResult();
const process =(): ng.IPromise<IResult> => {
for(let start = i; i < start + 100; ++i) {
const item = data[i];
if(!item) {
return $q.resolve(result);
}
result.add(handleItem(item));
}
return $timeout(process, 20)
}
return process();
}
这工作,但打字稿编译器抱怨,因为角$超时服务接受返回值的函数,而不是一个承诺:
TS2322:键入'IPromise < IPromise>'不可分配给'IPromise'类型。类型'IPromise < IResult>'中缺少属性'add'。
有没有一种方法来正确定义此函数中的类型?
我可以看到两个选项去:
- 定义过程返回
any
- 铸return语句:
return $timeout(process, 20) as any as ng.IPromise<IResult>
任何更好的主意吗?
它在这里解决了https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15364。 –