Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

like image 358
aleclarson Avatar asked Dec 06 '25 18:12

aleclarson


1 Answers

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 }>
}
like image 108
Titian Cernicova-Dragomir Avatar answered Dec 09 '25 14:12

Titian Cernicova-Dragomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!