This PR implements control flow analysis for dependent parameters and variables declared by destructuring discriminated unions. Specifically, when non-rest binding elements are declared as const variables or const-like parameters (parameters for which there are no assignments in the function body) and the parent type for the destructuring is a discriminated union type, conditional checks for variables destructured from discriminant properties now affect the types of other variables declared in the same destructuring.
Some examples:
type Action =
| { kind: ‘A’, payload: number }
| { kind: ‘B’, payload: string };
function f10({ kind, payload }: Action) {
if (kind === ‘A’) {
payload.toFixed();
}
if (kind === ‘B’) {
payload.toUpperCase();
}
}
function f11(action: Action) {
const { kind, payload } = action;
if (kind === ‘A’) {
payload.toFixed();
}
if (kind === ‘B’) {
payload.toUpperCase();
}
}
function f12({ kind, payload }: Action) {
switch (kind) {
case ‘A’:
payload.toFixed();
break;
case ‘B’:
payload.toUpperCase();
break;
default:
payload; // never
}
}
function f13(it: Iterator) {
const { value, done } = it.next();
if (!done) {
value; // number
}
}
Fixes #10830.
Fixes #35283.
Fixes #38020.
Fixes #46143.