Skip to content

Instantly share code, notes, and snippets.

@ValentinFunk
Created April 9, 2019 16:51
Show Gist options
  • Save ValentinFunk/85a8204b39e7c73f41c2eaf7c2ec2f57 to your computer and use it in GitHub Desktop.
Save ValentinFunk/85a8204b39e7c73f41c2eaf7c2ec2f57 to your computer and use it in GitHub Desktop.

Revisions

  1. ValentinFunk created this gist Apr 9, 2019.
    10 changes: 10 additions & 0 deletions discriminate.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    export type Discriminate<
    Union,
    Prop extends keyof Union,
    Discriminator extends Union[Prop],
    > = Union extends { [T in Prop]: Extract<Union[Prop], Discriminator> } ? Union : never;

    export type DiscriminateByType<
    Union extends { type: any },
    D extends Union['type']
    > = Discriminate<Union, 'type', D>;