Typescript Promise.all具有元组类型的包装器

Typescript Promise.all具有元组类型的包装器,typescript,typescript3.0,Typescript,Typescript3.0,我想创建一个使用元组类型的promise.all包装器。我正要写这样的东西: export function parallerRequest<TResponses extends any[]>(urls: string[]): Promise<TResponses> { return Promise.all<TResponses>(urls.map((url) => fetch(url))); } 导出函数parallerRequest(URL

我想创建一个使用元组类型的promise.all包装器。我正要写这样的东西:

export function parallerRequest<TResponses extends any[]>(urls: string[]): Promise<TResponses> {
    return Promise.all<TResponses>(urls.map((url) => fetch(url)));
}
导出函数parallerRequest(URL:string[]):Promise{ 返回Promise.all(url.map((url)=>fetch(url)); } 预期的行为应该是,当Promise.all被解析时,返回的数据将以提供的元组的形式出现。你知道如何实现这种行为吗?

即使
Promise.all()
对元组类型进行了正确的映射(至少在TypeScript 3.1发布之前是这样的),问题是编译器无法验证映射
fetch
url
的结果是否会产生
TResponses
类型的值。它只知道它将是
Response[]
类型,比
treponses
更宽。因此,
Promise.all
返回一个
Promise
。如果要告诉编译器不要担心这一点,可以使用:

所以这不是真正的类型安全。我不确定您指定
t响应的用例是什么,而不是只保留
Response[]
,但您应该小心


无论如何,我希望这能有所帮助。祝你好运

什么不起作用?您得到了什么错误?类型“Response”不可分配给类型“trespponses”。
export function parallelRequest<TResponses extends Response[]>(
  urls: string[]
): Promise<TResponses> {
    return Promise.all(urls.map(x => fetch(x))) as Promise<TResponses>; // okay
}
interface CustomResponse extends Response {
    prop: string;
}
parallelRequest<[Response, CustomResponse, Response]>(["/foo/bar", "/baz"]); // no error