[英]Typescript typings for Promise.all working with object
There is a counterpart for Promise.all
waiting for all promises in object. Promise.all
有一个对应Promise.all
等待对象中的所有承诺。 Sample implementation: https://www.npmjs.com/package/promise-all示例实现: https : //www.npmjs.com/package/promise-all
Usage:用法:
await promiseAll({
key1: Promise.resolve(1),
key2: Promise.resolve(2)
});
I wanted to write typings for such function.我想为这样的函数编写类型。 I tried using
keyof
but after a while, I noticed that I would somehow need to "unwrap" a type of a Promise which I doubt is possible.我尝试使用
keyof
但过了一会儿,我注意到我需要以某种方式“解开”一种我怀疑是可能的 Promise。
Here's what I came with which is actually just an identity mapping now :D这是我带来的,现在实际上只是一个身份映射:D
function promiseAll(o: object): { [P in keyof typeof o]: typeof o[P] } {
...
}
Any ideas?有任何想法吗?
Hah, I figured it out!哈,我想通了!
type Promised<T> = { [P in keyof T]: PromiseLike<T[P]> };
function promiseAll<T>(o: Promised<T>): T {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.