Skip to content

Commit 798257c

Browse files
committed
Merge pull request microsoft#8387 from Microsoft/controlFlowDestructuringParameter
Fix control flow analysis for destructuring parameters
2 parents 655e24c + 81bab64 commit 798257c

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7959,7 +7959,7 @@ namespace ts {
79597959
}
79607960
const declaration = localOrExportSymbol.valueDeclaration;
79617961
const defaultsToDeclaredType = !strictNullChecks || type.flags & TypeFlags.Any || !declaration ||
7962-
declaration.kind === SyntaxKind.Parameter || isInAmbientContext(declaration) ||
7962+
getRootDeclaration(declaration).kind === SyntaxKind.Parameter || isInAmbientContext(declaration) ||
79637963
getContainingFunctionOrModule(declaration) !== getContainingFunctionOrModule(node);
79647964
const flowType = getFlowTypeOfReference(node, type, defaultsToDeclaredType ? type : undefinedType);
79657965
if (strictNullChecks && !(type.flags & TypeFlags.Any) && !(getNullableKind(type) & TypeFlags.Undefined) && getNullableKind(flowType) & TypeFlags.Undefined) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [controlFlowDestructuringParameters.ts]
2+
// Repro for #8376
3+
4+
5+
[{ x: 1 }].map(
6+
({ x }) => x
7+
);
8+
9+
10+
//// [controlFlowDestructuringParameters.js]
11+
// Repro for #8376
12+
[{ x: 1 }].map(function (_a) {
13+
var x = _a.x;
14+
return x;
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/controlFlowDestructuringParameters.ts ===
2+
// Repro for #8376
3+
4+
5+
[{ x: 1 }].map(
6+
>[{ x: 1 }].map : Symbol(Array.map, Decl(lib.d.ts, --, --))
7+
>x : Symbol(x, Decl(controlFlowDestructuringParameters.ts, 3, 2))
8+
>map : Symbol(Array.map, Decl(lib.d.ts, --, --))
9+
10+
({ x }) => x
11+
>x : Symbol(x, Decl(controlFlowDestructuringParameters.ts, 4, 4))
12+
>x : Symbol(x, Decl(controlFlowDestructuringParameters.ts, 4, 4))
13+
14+
);
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/controlFlowDestructuringParameters.ts ===
2+
// Repro for #8376
3+
4+
5+
[{ x: 1 }].map(
6+
>[{ x: 1 }].map( ({ x }) => x) : number[]
7+
>[{ x: 1 }].map : <U>(callbackfn: (value: { x: number; }, index: number, array: { x: number; }[]) => U, thisArg?: any) => U[]
8+
>[{ x: 1 }] : { x: number; }[]
9+
>{ x: 1 } : { x: number; }
10+
>x : number
11+
>1 : number
12+
>map : <U>(callbackfn: (value: { x: number; }, index: number, array: { x: number; }[]) => U, thisArg?: any) => U[]
13+
14+
({ x }) => x
15+
>({ x }) => x : ({x}: { x: number; }) => number
16+
>x : number
17+
>x : number
18+
19+
);
20+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Repro for #8376
2+
3+
// @strictNullChecks: true
4+
5+
[{ x: 1 }].map(
6+
({ x }) => x
7+
);

0 commit comments

Comments
 (0)