I've encountered an issue with generated types. A particular API is providing me with two types, and I want to create distinct aliases for each.
In TypeScript, we can utilize Extract<>
to assist with this:
type Add = {
type: 'add';
id: string;
}
type Remove = {
type: 'remove';
id: string;
}
type Action = Add | Remove;
type addType = Extract<Action, {type: 'add'}>;
// ^? Add
However, the way my variations are defined differs:
type ActionsCombined = {
type: 'add' | 'remove';
id: string;
}
type addTypeCombined = Extract<ActionsCombined, {type: 'add'}>;
// ^? never
Although they appear equivalent to me, TypeScript doesn't recognize them as such. Is there a method to extract the add
variant from ActionsCombined
?
Alternatively, is there a way to transform the nested variant representation into two independent variants?