简体   繁体   English

Typescript:键入带有函数数组的 function,该函数返回每个 function 返回类型的数组

[英]Typescript: typing a function with an array of functions that returns an array of each function return type

Is it possible for the code below to be typed correctly?下面的代码是否可以正确输入?

function arrayElementTypes(...array: Array<(() => string) | (() => number) | (() => {prop: string}) | (() => number[])>) {
   /// .. do something
   /// .. and then return an array with each functions result
   return array.map(arg => arg())
}

const [varA, varB, varC] = arrayElementTypes( () => "", () => ({prop: "prop"}), () => [1,2,3] )
// how can this be typed appropriately so that the : 
// varA: string
// varB: {prop: string}
// varC: number[]

Managed to do it with设法做到了

type ReturnTypes<T extends Array<(...a: any[]) => any>> = {
  [P in keyof T]: T[P] extends (...a: any[]) => infer R ? R : never
}

type CustomArrayElement = (() => string) | (() => number) | (() => {prop: string}) | (() => number[])

function arrayElementTypes<T extends CustomArrayElement[]>(...array: T): ReturnTypes<typeof array> {
   return array.map(arg => arg())
}

const [varA, varB, varC] = arrayElementTypes( () => "", () => ({prop: "prop"}), () => [1,2,3] )

Thank you all for your help!!谢谢大家的帮助!!

I'm not optimistic that it can be typed correctly.我不乐观它可以正确输入。 Firstly, I think you are looking at a tuple , rather than an array, since the "array" of functions have different signatures, and you want the return type of arrayElementTypes to correspond to the return type of each function in the "array", matching position-wise .首先,我认为您正在查看tuple ,而不是数组,因为函数的“数组”具有不同的签名,并且您希望arrayElementTypes的返回类型对应于“数组”中每个 function 的返回类型,按位置匹配。

At the same time, I hesitate to say it's impossible outright, as I've seen amazing things that can be done using a combination of generics and conditional types.同时,我不敢说这完全不可能,因为我已经看到了可以使用 generics 和条件类型的组合来完成的惊人事情


Edit: I have come up with some "building-block" types that may help with the final answer, but you can see if you can piece them together:)编辑:我想出了一些可能有助于最终答案的“构建块”类型,但您可以看看是否可以将它们拼凑在一起:)

// any function
type Fn = () => unknown;

// a tuple/array of functions
type FnArr = readonly Fn[];

// the first function in your tuple of functions
type Head<T extends FnArr> = T extends [infer HeadFn, ...any[]] ? HeadFn : never;

// the rest of the functions in your tuple of functions
type Tail<T extends FnArr> = T extends [any, ...infer TailFns] ? TailFns : never;

Using the above building blocks, you can extract the return types of each function in your tuple of functions.使用上述构建块,您可以提取函数元组中每个 function 的返回类型。 This doesn't directly lend itself to a solution, but maybe some recursively defined conditional types (to generalise for arbitrary tuple of functions) can get you there:)这并不直接适用于解决方案,但也许一些递归定义的条件类型(概括为任意函数元组)可以让你到达那里:)

const [varA, varB, varC] = arrayElementTypes( () => "", () => ({prop: "prop"}), () => [1,2,3] )
// how can this be typed appropriately so that the : 
// varA: string
// varB: {prop: string}
// varC: number[]

type ExampleFns = [ () => string, () => {prop: "prop"}, () => number[] ];

type TypeForVarA = ReturnType<Head<ExampleFns>>;                // F1 = string
type TypeForVarB = ReturnType<Head<Tail<ExampleFns>>>;          // F2 = {prop: "prop"}
type TypeForVarC = ReturnType<Head<Tail<Tail<ExampleFns>>>>;    // F3 = number[]
const [varA, varB, varC, varD]: Array<string | number | {prop: string} | number[]> = arrayElementTypes( () => "", () => ({prop: "prop"}), () => [1,2,3] )

Does this satisfy your requirements?这是否满足您的要求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM