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

问题描述 投票:4回答:1

我需要向一个函数添加一个类型声明,该函数将元组[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'.
typescript generics types tuples
1个回答
6
投票

更新:从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的元组,并且您可以在顶部添加其他重载以实现您在实践中所需的任何内容。这是冗长而丑陋的,但应该有效。

希望有所帮助;祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.