【问题标题】:Generics for Arrays in TypeScript 3.0TypeScript 3.0 中的数组泛型
【发布时间】:2019-01-11 11:26:30
【问题描述】:

所以我看到 3.0 带有用于其余参数的通用类型,因此您可以执行以下操作:

static identity<T extends any[]>(...values: T): T;

是否有可能为数组参数获得类似的东西,或者目前有什么我不知道的工作?例如,如果您查看es6-promise declarations

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 的规范是它接受一个可迭代的而不是休息参数。不必写出所有这些类型真是太好了,但是根据我对 TypeScript 3.0 的第三知识,这还不可能。我说的对吗?

【问题讨论】:

  • in static identity&lt;T extends any[]&gt;(...values: T): T; T 将是基于输入参数的元组类型。我试图映射一个元组类型(对元组类型中的每个项目应用一些转换),但这似乎目前没有得到很好的支持。
  • Promise.all 提出了一个额外的问题,因为您可以传入T | PromiseLike&lt;T&gt; 的数组,所有的承诺都将得到解决。但是,TS 3.0 似乎并没有解决数组参数作为泛型元组的主要问题。
  • @TitianCernicova-Dragomir 如果您不知道,tuple mapping 应该在 TS 3.1 中得到完全支持。并不是说这完全解决了这个问题,因为它要求在没有 rest 参数的情况下为类似数组的输入推断一个元组。
  • @jcalz 哇,10 倍的信息,这是个好消息。我有点惊讶它不在路线图上,这似乎是一个非常重要的改进!
  • 仅供参考,我更新了我的答案,以展示如何在调用者无需执行任何操作的情况下将数组文字推断为元组。

标签: typescript typescript3.0


【解决方案1】:

2019 年 10 月 6 日更新:是的,这在 TS3.1 中是可能的。

请参阅this issuethis comment,了解在您尝试推断的上下文中包含元组类型的技巧。像这样:

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 被推断为一个元组。它的工作原理:

declare const thenableString: Thenable<string>;
const a = all(["hey", thenableString, 123]); // Promise<[string, string, number]>;

link to code in Playground


下面是旧答案:

我认为 TypeScript 3.1 将引入 tuple mapping,这将有助于其中的一部分。但不幸的是,我不知道如何在不使用 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]>;

如果我找到更好的解决方案,我会考虑更多并编辑答案。


更新:目前看来不可能。值不会被推断为元组。这是一个long-standing issue,(最初)被认为是故意行为......现在元组变得越来越强大,有another more recent GitHub issue 询问这个问题,并具体提到了cmets 中Promise.all 如何需要它.如果你关心它,你可能想去那个问题并给它一个?或参与讨论。

希望对您有所帮助。祝你好运。

【讨论】:

    猜你喜欢
    • 2019-12-08
    • 2012-10-04
    • 2023-02-09
    • 2019-01-23
    • 2021-03-15
    • 2023-04-04
    • 2019-05-15
    • 2022-11-18
    • 2017-06-02
    相关资源
    最近更新 更多