TypeScript Version: nightly (2.0.0-dev.201xxxxx)
Code
typeToString={toString(): string;}typeBoxedValue={kind: 'int',num: number}|{kind: 'string',str: string}typeIntersectionFail=BoxedValue&ToStringtypeIntersectionInline={kind: 'int',num: number}&ToString|{kind: 'string',str: string}&ToStringfunctiongetValueAsString(value: IntersectionFail): string{if(value.kind==='int'){// Property 'num' does not exist on type '({ kind: "int"; num: number; } | { kind: "string"; str: string; }) & { toString(): string; }'.return''+value.num;}// Property 'str' does not exist on type '({ kind: "int"; num: number; } | { kind: "string"; str: string; }) & { toString(): string; }'.returnvalue.str;}If a discriminated union is enhanced via intersection, type narrowing based on the discrimination field starts to fail. This behaviour is not exhibited if a new discriminated union is created with the intersection being inlined into each of the respective options.
The function getValueAsString(value: IntersectionFail): string fails to compile, while getValueAsString(value: IntersectionInline): string compiles just fine. Unfortunately, it is IntersectionFail that is the result of natural program evolution.
TypeScript Version: nightly (2.0.0-dev.201xxxxx)
Code
If a discriminated union is enhanced via intersection, type narrowing based on the discrimination field starts to fail. This behaviour is not exhibited if a new discriminated union is created with the intersection being inlined into each of the respective options.
The function
getValueAsString(value: IntersectionFail): stringfails to compile, whilegetValueAsString(value: IntersectionInline): stringcompiles just fine. Unfortunately, it isIntersectionFailthat is the result of natural program evolution.