0

Is it possible to remap type of array (or maybe tuple?)

Let's say that I have an array of functions and I want to make of it array of functions captured parameters. Something like.

type Remap<F extends ((... args: any) => any)[]> =
    F extends [infer T1] ? [Parameters<T1>] :
    F extends [infer T1, infer T2] ? [Parameters<T1>, Parameters<T2>] :
    F extends [infer T1, infer T2, infer T3] ? [Parameters<T1>, Parameters<T2>,  Parameters<T3>] :
    never;

const a = [(a: string) => void, (b: number) => void]
type T = Remap<typeof a> // [[string], [number]] - simply captured arguments of functions.

What i need to is pass array of functions like "a" and create maybe new function which will contain parameters from all functions like ([first arguments], [second arguments]).

1 Answer 1

1

You can use a mapped type to map a tuple:

type Remap<F extends ((...args: any) => any)[]> = {
  [P in keyof F]: F[P] extends (...a: infer P) => any ? P: never
}

declare const a: [(a: string) => void, (b: number) => void]
type T = Remap<typeof a>

Playground Link

Sign up to request clarification or add additional context in comments.

3 Comments

@luky wops, initially repasted the same code..hope you saw the updated mapped tuple version (it was always in the playground link..)
yes i saw it there, but first i was looking for the changes :)
and (...params: T) is [[string], [number]] and if i wanted [string, number]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.