Control flow analysis for dependent parameters (#47190)

* Support control flow analysis for dependent parameters

* Add tests
This commit is contained in:
Anders Hejlsberg
2022-01-04 09:22:11 -10:00
committed by GitHub
parent 2f058b72d6
commit f4e1efbc29
5 changed files with 620 additions and 43 deletions

View File

@@ -170,3 +170,61 @@ const { value, done } = it.next();
if (!done) {
value; // number
}
// Repro from #46658
declare function f50(cb: (...args: Args) => void): void
f50((kind, data) => {
if (kind === 'A') {
data.toFixed();
}
if (kind === 'B') {
data.toUpperCase();
}
});
const f51: (...args: ['A', number] | ['B', string]) => void = (kind, payload) => {
if (kind === 'A') {
payload.toFixed();
}
if (kind === 'B') {
payload.toUpperCase();
}
};
const f52: (...args: ['A', number] | ['B']) => void = (kind, payload?) => {
if (kind === 'A') {
payload.toFixed();
}
else {
payload; // undefined
}
};
declare function readFile(path: string, callback: (...args: [err: null, data: unknown[]] | [err: Error, data: undefined]) => void): void;
readFile('hello', (err, data) => {
if (err === null) {
data.length;
}
else {
err.message;
}
});
type ReducerArgs = ["add", { a: number, b: number }] | ["concat", { firstArr: any[], secondArr: any[] }];
const reducer: (...args: ReducerArgs) => void = (op, args) => {
switch (op) {
case "add":
console.log(args.a + args.b);
break;
case "concat":
console.log(args.firstArr.concat(args.secondArr));
break;
}
}
reducer("add", { a: 1, b: 3 });
reducer("concat", { firstArr: [1, 2], secondArr: [3, 4] });