[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">
*/

 

posted @ 2023-09-02 19:12  Zhentiw  阅读(4)  评论(0编辑  收藏  举报