TypeScript Version: master
Search Terms:
Discriminated union intersection narrowing
Code
type A = { key: "one", value: number } | { key: "two", value: string };
type A_one = A & { key: "one" };
function foo(a1: A_one): number {
if (a1.key) {
}
return a1.value; // no error
}
function bar(a1: A_one): number {
return a1.value; // error
}
Expected behavior:
Consistent behavior between foo and bar, though I'm not sure how easy that is.
- Making them both error feels like a regression.
- Making them both ok would need to introduce some new logic for arbitrary property access.
Actual behavior:
The control flow through the joint point is doing some type reduction not present in the plain version.
Playground Link:
link
Related Issues:
Found when looking at #36323.
This was (probably) introduced by the fix at #35513.