【问题标题】:Typescript typings for Promise.all working with objectPromise.all 的打字稿类型与对象一起使用
【发布时间】:2017-10-22 20:25:04
【问题描述】:

有一个对应的 Promise.all 等待对象中的所有承诺。示例实现:https://www.npmjs.com/package/promise-all

用法:

await promiseAll({ 
    key1: Promise.resolve(1), 
    key2: Promise.resolve(2) 
});

我想为这样的功能写打字。我尝试使用keyof,但过了一会儿,我注意到我需要以某种方式“解开”一种我怀疑可能的 Promise。

这是我带来的,现在实际上只是一个身份映射:D

function promiseAll(o: object): { [P in keyof typeof o]: typeof o[P] } {
...
}

有什么想法吗?

【问题讨论】:

标签: javascript typescript types


【解决方案1】:

哈,我想通了!

type Promised<T> = { [P in keyof T]: PromiseLike<T[P]> };
function promiseAll<T>(o: Promised<T>): T {
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 2018-09-15
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多