I
I
ivan05122020-05-07 12:19:21
typescript
ivan0512, 2020-05-07 12:19:21

Is it possible to take a type from a union?

Tell me, is there a similar type, is it possible to somehow pull out one of the subtypes by passing the type number as a generic? To work like this?
type Union = { type: 1 } | { type: 2 };

type Result = PickType<Union, 1> // result = { type: 1 }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Pastukhov, 2020-05-07
@ivan0512

Extract
type Result = Extract<Union, { type: 1 }>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question