Assuming I have this type declaration:
type Foo = 'a' | 'b' | 'c';
type Bar = 'a' | 'b' ;
Is it possible to express Bar
as a subset of Foo
?
I understand it's always possible to express Foo
as a superset of Bar
, but in my case the other way around would feel more in line with the domain.
You simply have to use the Exclude
pre-defined conditional type:
type Foo = 'a' | 'b' | 'c';
type Bar = Exclude<Foo, 'c'>;
const Bar = 'a';
Do note that the following works fine, even if it may not feel right at first glance:
type Bar = Exclude<Foo, 'd'>
See playground.
You can also combine it with index types for interesting purposes:
type Foo = 'a' | 'b' | 'c';
type AnObject = { c: boolean }
type Bar = Exclude<Foo, keyof AnObject>
const myVar: Bar = "a";
E.g.
type Bar = Exclude<Foo, 'c'>
(documented in https://www.typescriptlang.org/docs/handbook/advanced-types.html#predefined-conditional-types)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With