[Typescript] DistributiveOmit
Omit on Union type
type Union =
| {
a: "a";
user?: string;
}
| {
b: "b";
user?: string;
};
type X = Omit<Union, "user">; // X is {}
Using DistributiveOmit
:
type DistributiveOmit<T, TOmitted extends PropertyKey> = T extends any
? Omit<T, TOmitted>
: never;
type Union =
| {
a: "a";
user?: string;
}
| {
b: "b";
user?: string;
};
type X = DistributiveOmit<Union, "user">;
/*
Omit<{
a: "a";
user?: string | undefined;
}, "user"> | Omit<{
b: "b";
user?: string | undefined;
}, "user">
*/