lmonninger
lmonninger

Reputation: 961

How can I map an array of types to another array of types in Typescript?

Let's say I have this:

type CoolTuple = [string, number, boolean]

And, I want to map that type using some generic:

type CoolGeneric<T extends any[]> = ... 

Such that:

CoolGeneric<CoolType> is [Wrapper<string>, Wrapper<number>, Wrapper<boolean>] 

What would I do?

Upvotes: 0

Views: 623

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 187312

You want a mapped type:

type CoolGeneric<T extends any[]> = { [K in keyof T]: Wrapper<T[K]> }

Basically, for each key (indices of a tuple are keys), declare the type to be something that uses the property type T[K].

Playground

Upvotes: 2

Related Questions