TypeScript中用于元组的通用类型包装

时间:2017-09-02 10:49:39

标签: typescript generics types tuples

我需要向一个函数添加一个类型声明,该函数将元组[Foo<A>, Foo<B>, ...]的元素映射到函数() => [A, B, ...]。我怎样才能在TypeScript中实现这一目标?

此示例在结构上与应用程序的相关部分类似:

interface SomethingWithAValue<T> { value: T; }

function collect(array) {
  return () => array.map(a => a.value);
}

返回与每个对象关联的值的元组。 collect的类型声明会是什么样的?

伪代码:

function collect<[SomethingWithAValue<T>...](array: [SomethingWithAValue<T>...]): () => [T...];

根据jonrsharpe的建议更新:

interface SomethingWithAValue<T> { value: T; }

function collect<T>(array: SomethingWithAValue<T>[]): () => T[] {
  return () => array.map(a => a.value);
}

type myTupleType = [string, number];

let somethings: [SomethingWithAValue<string>, SomethingWithAValue<number>];
somethings = [{ value: 'foo' }, { value: 5 }];

let fn: () => myTupleType = collect(somethings);

这不起作用:

Argument of type '[SomethingWithAValue<string>, SomethingWithAValue<number>]' is not assignable to parameter of type 'SomethingWithAValue<string>[]'.
  Types of property 'pop' are incompatible.
    Type '() => SomethingWithAValue<string> | SomethingWithAValue<number>' is not assignable to type '() => SomethingWithAValue<string>'.
      Type 'SomethingWithAValue<string> | SomethingWithAValue<number>' is not assignable to type 'SomethingWithAValue<string>'.
        Type 'SomethingWithAValue<number>' is not assignable to type 'SomethingWithAValue<string>'.
          Type 'number' is not assignable to type 'string'.

1 个答案:

答案 0 :(得分:3)

更新:从TS3.1开始,以下答案已经过时。我相信您现在可以使用mapped tuple typesconditional type inference来获得您想要的行为:

type ExtractValue<T extends ReadonlyArray<SomethingWithAValue<any>>> =
  { [K in keyof T]: T[K] extends SomethingWithAValue<infer V> ? V : never };

function collect<T extends ReadonlyArray<SomethingWithAValue<any>>>(
  array: T
): () => ExtractValue<T> {
  return () => array.map(a => a.value) as any;
}

让我们使用它...首先让我们很容易得到一个带有辅助函数tuple()的元组类型,它接受一个可变数量的参数并输出一个元组(这在TS3中是可能的。 0)

type Narrowable = string | number | boolean | undefined | null | void | {};
const tuple = <T extends Narrowable[]>(...t: T) => t;

让我们看看它是否有效:

const result = collect(tuple({ value: 10 }, { value: "hey" }, { value: true }));
// const result: () => [number, string, boolean]

看起来不错!

老答案:

TypeScript中没有variadic kinds,因此无法输入通用元组(不存在语法[T...])。

作为一种变通方法,您可以为任何长度的元组提供函数重载,直到达到一定的合理最大值:

function collect<A, B, C, D, E>(array: [SomethingWithAValue<A>, SomethingWithAValue<B>, SomethingWithAValue<C>, SomethingWithAValue<D>, SomethingWithAValue<E>]): () => [A, B, C, D, E];
function collect<A, B, C, D>(array: [SomethingWithAValue<A>, SomethingWithAValue<B>, SomethingWithAValue<C>, SomethingWithAValue<D>]): () => [A, B, C, D];
function collect<A, B, C>(array: [SomethingWithAValue<A>, SomethingWithAValue<B>, SomethingWithAValue<C>]): () => [A, B, C];
function collect<A, B>(array: [SomethingWithAValue<A>, SomethingWithAValue<B>]): () => [A, B];
function collect<A>(array: [SomethingWithAValue<A>]): () => [A];
function collect<T>(array: SomethingWithAValue<T>[]): () => T[] {
  // implementation
}

这应该适用于长度为5的元组,并且您可以在顶部添加其他重载以实现您在实践中所需的任何内容。这是冗长而丑陋的,但应该有效。

希望有所帮助;祝你好运!