TypeScript:Promise.all 不处理联合类型

dot*_*ral 6 promise typescript union-types

我有以下一段代码导致 TypeScript 编译错误:

type Promisable = (() => Promise<string>) | (() => Promise<number>);

const func = async (promisable: Promisable) => {
  await Promise.all([promisable()]);
};
Run Code Online (Sandbox Code Playgroud)

错误如下

没有重载匹配这个调用。最后一次重载给出了以下错误。'(Promise | Promise)[]' 类型的参数不可分配给 'Iterable>' 类型的参数。'Symbol.iterator.next(...)' 返回的类型在这些类型之间不兼容。

作为记录,删除联合类型按预期工作:

type Promisable = () => Promise<string>;

const func = async (promisable: Promisable) => {
  await Promise.all([promisable()]);
};
Run Code Online (Sandbox Code Playgroud)

你可以在这里看到的错误自己https://www.typescriptlang.org/play/?ssl=4&ssc=3&pln=1&pc=1#code/C4TwDgpgBACgTgewLYEsDOBDARgG2gXigAoiBKKfAPlkVTQgB41g4UA7Ac0vIB9iyK1eMnSM2AVyRYIcbgG4AsACgAxgjbMoAM3FsVFKBjQg9xMLXTY8ALhojMuCOSpQA3sqiGA7hhTA7dBAAdBg4OEQA2ub2VhBkALqkikoAvnJAA

是否不能将联合类型与Promise.all?

编辑:我知道可以使用类似的东西() => Promise<string|number>。但是在一个异步函数很多、类型很大的高级应用中,把函数的联合转换成联合的函数并不容易。从代码的角度来看,它也不是很实用。

for*_*d04 6

更新

这是使用当前promise 类型声明进行类型推断失败的情况之一。最简单的解决方案是手动添加泛型类型参数:

const promisable: Promisable = ...
const res = await Promise.all<string | number>([promisable()]); 
// res: (string|number)[]
Run Code Online (Sandbox Code Playgroud)

您可能会string | number自动推断:

type PromiseReturn<T> = T extends () => Promise<infer I> ? I : never
const res = await Promise.all<PromiseReturn<Promisable>>([promisable()]);
Run Code Online (Sandbox Code Playgroud)

使用TypeScript 4.1:更复杂的、潜在嵌套的 Promise 类型可以通过自定义递归Awaited类型来解析和扁平化,如下所示:

type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;
Run Code Online (Sandbox Code Playgroud)

操场



旧答案

更新:该awaited类型的操作被推迟到以后的版本-尚不清楚是否会在所有被释放。


这是一个已知问题。好消息:TS 3.9(即将推出测试版)将推出改进的承诺类型:

我想重新引入#17077 中的awaited类型运算符,以满足我们对递归解包类似 Promise 类型的机制的需求,例如, , , , 和。Promise.allPromise.racePromise.allSettledPromise.prototype.thenPromise.prototype.catch

类型声明的Promise.all和其他人使用的新awaited类型的操作。如果您使用夜间构建进行测试,Promise.all现在可以正确解析为Promise<(string | number)[]>:

type Promisable = (() => Promise<string>) | (() => Promise<number>);

declare const promisable: Promisable
const res = await Promise.all([promisable()]); // res: (string | number)[]
Run Code Online (Sandbox Code Playgroud)

相比之下,TS 3.8处理不了。对于版本 < 3.9,您可以手动分配泛型类型参数:

declare const promisable: Promisable
const res = await Promise.all<string | number>([promisable()]); // res: (string | number)[]
Run Code Online (Sandbox Code Playgroud)