Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pick from union type string

Tags:

typescript

I have a union type, and I'd like to pick certain values from the union type. Is this possible? I've tried to use 'Pick', but this doesn't work on the union type.

e.g.

type FooType = 'foo' | 'bar' | 'baz';
type Extracted = : Pick<FooType, 'foo' | 'bar'>; // should contains only 'foo' and 'bar'

I've now tried various strategies, but cannot get this to work.

like image 584
CookieEater Avatar asked Dec 07 '25 13:12

CookieEater


1 Answers

Try using Extract instead of Pick:

type FooType = 'foo' | 'bar' | 'baz';
type Extracted = Extract<FooType, 'foo' | 'bar'>;
// type Extracted = 'foo' | 'bar'
like image 164
mightimaus Avatar answered Dec 09 '25 16:12

mightimaus