Skip to content

Instantly share code, notes, and snippets.

@sanex3339
Created July 18, 2019 07:49
Show Gist options
  • Save sanex3339/85822811de5722c24197c0d7a7b5c6e2 to your computer and use it in GitHub Desktop.
Save sanex3339/85822811de5722c24197c0d7a7b5c6e2 to your computer and use it in GitHub Desktop.
type UnionKeys<T> = T extends any ? keyof T : never;
type StrictUnionHelper<T, TAllKeys extends PropertyKey> = T extends any
? T & Partial<Record<Exclude<TAllKeys, keyof T>, never>>
: never;
/**
* Тип для правильной работы Discriminated Union типов
* https://stackoverflow.com/questions/52677576/typescript-discriminated-union-allows-invalid-state/52678379#52678379
*/
export type StrictUnion<T> = StrictUnionHelper<T, UnionKeys<T>>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment