首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeScript 3.0中数组的泛型

TypeScript 3.0中数组的泛型
EN

Stack Overflow用户
提问于 2018-08-03 14:28:22
回答 1查看 8.1K关注 0票数 5

因此,我看到3.0附带了rest参数的通用类型,所以您可以这样做:

代码语言:javascript
运行
复制
static identity<T extends any[]>(...values: T): T;

是否有可能为数组参数获得类似的东西,还是目前有一些我不知道的东西在工作呢?例如,如果您查看ES6-承诺声明

代码语言:javascript
运行
复制
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
static all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
static all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
static all<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
static all<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>]): Promise<[T1, T2, T3, T4, T5]>;
static all<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>]): Promise<[T1, T2, T3, T4]>;
static all<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): Promise<[T1, T2, T3]>;
static all<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): Promise<[T1, T2]>;
static all<T1>(values: [T1 | Thenable<T1>]): Promise<[T1]>;
static all<TAll>(values: Array<TAll | Thenable<TAll>>): Promise<TAll[]>;

Promise.all的规范是接受一个可迭代的参数,而不是rest参数。不用把所有这些类型都写出来真是太好了,但从我对TypeScript 3.0的第三层知识来看,这还不可能。我说的对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-03 19:27:49

更新2019-10-06:是的,这在TS3.1中是可能的.

有关将元组类型包含在您要推断的内容的上下文中的技巧,请参见本期这句话。如下所示:

代码语言:javascript
运行
复制
declare function all<T extends any[] | []>( // note | [] here
  values: T
): Promise<{ [K in keyof T]: T[K] extends Thenable<infer R> ? R : T[K] }>;

请注意,类型any[] | []any[]没有太大区别(类型[]可以分配给any[],因此any[] | []实际上是与any[]相同的一组类型),但是如果可能的话,使用前面提到的空元组[]会提示编译器希望T被推断为元组。信息技术的作用是:

代码语言:javascript
运行
复制
declare const thenableString: Thenable<string>;
const a = all(["hey", thenableString, 123]); // Promise<[string, string, number]>;

链接到游乐场中的代码

旧的答案如下:

我认为TypeScript 3.1将引入元组映射,这将在一定程度上对此有所帮助。但不幸的是,我不知道如何说服类似数组的函数参数在不使用rest参数的情况下被推断为元组。我现在最接近您想要的(使用typescript@next来获得映射的元组)是:

代码语言:javascript
运行
复制
declare function all<T extends any[]>(
  values: T
): Promise<{[K in keyof T]: T[K] extends Thenable<infer R> ? R : T[K]}>;
function tuple<T extends any[]>(...args: T) { return args};
declare const thenableString: Thenable<string>;
const a = all(tuple("hey",thenableString,123)); // Promise<[string, string, number]>;

如果我找到一个更好的解决方案,我会更多地考虑这个问题并编辑答案。

更新:目前看来不可能。值只是不被推断为元组。这是一个最初被认为是故意行为的长期存在的问题 .现在元组变得更强大了,有另一个最近的GitHub问题在询问这个问题,并在注释中特别提到了Promise.all如何需要元组。如果你关心这个问题,你可能想去讨论这个问题,或者参加讨论。

希望这是有帮助的。祝好运。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51674820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档