Description
TypeScript Version: 3.5.0-rc, 3.5.0-dev.20190521
Search Terms: discriminated union omit, tagged union omit
Code
// discriminated union
type Union =
| { type: 'A', a: 'a', c: string }
| { type: 'B', b: 'b', c: string };
// various Omit definitions
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; // new omit copied from TS 3.5
type OmitBetter<T, K extends keyof any> = T extends any ? Pick<T, Exclude<keyof T, K>> : never;
type OmitBetterStrict<T, K extends keyof T> = T extends any ? Pick<T, Exclude<keyof T, K>> : never;
// Omit use-cases with Discriminated Union (DU)
type UnionMod = Omit<Union, 'whatever'>; // DU gets broken
type UnionMod2 = OmitBetter<Union, 'a'>; // DU remains unchanged
type UnionMod3 = OmitBetter<Union, 'a'>; // DU is still a DU but without field 'a'
type UnionMod4 = OmitBetterStrict<Union, 'c'>; // DU is still a DU but without common field 'c'
type UnionMod5 = OmitBetterStrict<Union, 'a'>; // error, because 'a' is not a common field of DU
// regular Omit use-cases (they all work properly)
type Regular = Omit<{ readonly a: 'a'; b?: 'b'; c: 'c'; }, 'whatever' | 'c'>;
type Regular2 = OmitBetter<{ readonly a: 'a'; b?: 'b'; c: 'c'; }, 'whatever' | 'c'>;
type Regular3 = OmitBetterStrict<{ readonly a: 'a'; b?: 'b'; c: 'c'; }, 'c'>;
Expected behavior:
Omit
type should properly omit fields from objects but discriminated unions should still be discriminated unions. The expected behaviour is observed when using Omit
from type-zoo package. The same approach was also proposed by the Microsoft member here: #28791 (comment).
Actual behavior:
Built-in Omit
type which is being introduced in TypeScript 3.5 "merges" discriminated unions. When omitting field c
from
{ type: 'A', a: 'a', c: string } |
{ type: 'B', b: 'b', c: string }
the resulting type should be equivalent to
{ type: 'A', a: 'a' } |
{ type: 'B', b: 'b' }
but with [email protected], we get
{ type: 'A' | 'B' }
which is no longer a discriminated union.
According to Design Meeting Notes, 4/15/2019 (#30947), the resolution about making Omit
stricter was based on "not worth breaking half the usages of Omit
in the wild". When looking at "type Omit" search results, some of them also use the conditional type trick to distribute Omit
over discriminated union. Following the same "not breaking the usages in the wild" principle, it might be a better idea to change the built-in Omit
into the version with T extends any ? ... : never
. Especially since it does not break the regular use-cases.
Playground Link: Link