wjo*_*sto 3 typescript typescript3.0
因此,我看到3.0附带了rest参数的通用类型,因此您可以执行以下操作:
static identity<T extends any[]>(...values: T): T;
Run Code Online (Sandbox Code Playgroud)
是否有可能为数组参数获取类似的东西,或者当前是否有我不知道的东西在工作?例如,如果您查看es6-promise声明
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[]>;
Run Code Online (Sandbox Code Playgroud)
规范Promise.all是,它接受一个可迭代的参数,而不是其他参数。不必写出所有这些类型确实很不错,但是根据我对TypeScript 3.0的专业知识,目前还不可能。我对么?
更新2019-10-06:是的,从TS3.1开始是可能的。
请参见此问题和此注释,以获取在您要推断的内容中包括元组类型的技巧。像这样:
declare function all<T extends any[] | []>( // note | [] here
values: T
): Promise<{ [K in keyof T]: T[K] extends Thenable<infer R> ? R : T[K] }>;
Run Code Online (Sandbox Code Playgroud)
请注意,类型any[] | []与any[](类型[]可分配给any[],所以any[] | []类型实际上与)没有什么不同any[],但是[]提到了空元组后,编译器便会暗示您想T将其推断为元组如果可能的话。和它的工作原理:
declare const thenableString: Thenable<string>;
const a = all(["hey", thenableString, 123]); // Promise<[string, string, number]>;
Run Code Online (Sandbox Code Playgroud)
请参阅Playground中的代码链接。
下面的旧答案:
我认为TypeScript 3.1将引入元组映射,这将对此有所帮助。但是不幸的是,我不知道一种说服不使用rest参数将类似数组的函数参数推断为元组的方法。我现在最接近您想要的(typescript@next用来获取映射元组)的是:
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]>;
Run Code Online (Sandbox Code Playgroud)
如果我找到更好的解决方案,我会考虑更多,并编辑答案。
更新:目前看来不可能。值只是不会被推断为元组。这是一个长期存在的问题,(最初)被认为是有意的行为...随着元组变得越来越强大,存在另一个最近的GitHub问题,有关此问题,并Promise.all在注释中具体说明了它的需要。如果您关心它,则可能要转到该问题上并给它打个电话或参加讨论。
希望对您有所帮助。祝好运。
| 归档时间: |
|
| 查看次数: |
690 次 |
| 最近记录: |