aleclarson
aleclarson

Reputation: 19045

Convert discriminated union (or array of object types) into a mapped type with a literal property as the key

Can I convert an Array<A | B> type into { [key: (A | B)['type']]: A | B } where type "a" maps to type A, and type "b" maps to type B.

type A = {type: 'a'}
type B = {type: 'b'}

type Kinds = [A, B]
type Kind = Kinds[number]

// How to use the above types to get this?
type ByKind = { a: A, b: B }

I want to avoid explicitly declaring each key of the ByKind object type, since they are already declared within types A and B.

Upvotes: 3

Views: 198

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250056

You were pretty close, we can use a mapped type to map over the union of string literals in Kind[type] but we then need to use the Extract conditional type to extract the type from the union that fits the key P

type A = {type: 'a'}
type B = {type: 'b'}

type Kinds = [A, B]
type Kind = Kinds[number]

type ByKind = {
    [P in Kind['type']]: Extract<Kind, { type: P }>
}

Upvotes: 4

Related Questions