Reputation: 69
This was my best attempt at trying this. Is it possible or even necessary? Maybe there is something I'm missing about typescript that makes more sense.
const TrueOrFalse = True | False;
const OnlyTrue = Omit<TrueOrFalse, false>;
Upvotes: 1
Views: 1058
Reputation: 249686
Omit
removes properties from an object type. What you want to do is exclude a type from a union. You can do this with the built-in conditional type Exclude
.
type X = Exclude<boolean, true> // X is false
Exclude
will take out of the first type parameter, any type that is a subtype of the second parameter.
You can read more about exclude here
Exclude
and its cousin Extract
can be useful also in manipulating discriminated unions, to extract or exclude a specific constituent from a union, even if you only know the discriminant not the whole the type:
type Shape = { type: "sq", size: number } | { type: "circle", radius: number }
type Square = Exclude<Shape, { type: "circle" }> // { type: "sq", size: number }
type Circle = Extract<Shape, { type: "circle" }> // { type: "circle", radius: number }
Upvotes: 4