Skip to content

Commit 917cd6c

Browse files
authored
Merge pull request microsoft#32035 from microsoft/fixTupleDestructuringControlFlow
Fix tuple destructuring control flow analysis
2 parents 2c458c0 + 3ca2e7d commit 917cd6c

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14440,7 +14440,7 @@ namespace ts {
1444014440
if (propType) {
1444114441
return propType;
1444214442
}
14443-
if (everyType(type, isTupleType) && !everyType(type, t => !(<TupleTypeReference>t).target.hasRestElement)) {
14443+
if (everyType(type, isTupleType)) {
1444414444
return mapType(type, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
1444514445
}
1444614446
return undefined;

tests/baselines/reference/destructuringControlFlow.errors.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(31,8): error TS2339: Property 'x' does not exist on type 'Number'.
22
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(32,9): error TS2339: Property 'x' does not exist on type 'Number'.
33
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(33,9): error TS2537: Type 'Number' has no matching index signature for type 'string'.
4+
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(40,1): error TS2532: Object is possibly 'undefined'.
45

56

6-
==== tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts (3 errors) ====
7+
==== tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts (4 errors) ====
78
function f1(obj: { a?: string }) {
89
if (obj.a) {
910
obj = {};
@@ -44,4 +45,12 @@ tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(33,9): err
4445
~~~~~~~~
4546
!!! error TS2537: Type 'Number' has no matching index signature for type 'string'.
4647
}
48+
49+
// Repro from #31770
50+
51+
type KeyValue = [string, string?];
52+
let [key, value]: KeyValue = ["foo"];
53+
value.toUpperCase(); // Error
54+
~~~~~
55+
!!! error TS2532: Object is possibly 'undefined'.
4756

tests/baselines/reference/destructuringControlFlow.js

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ function f4() {
3333
({ ["x"]: x } = 0); // Error
3434
({ ["x" + ""]: x } = 0); // Errpr
3535
}
36+
37+
// Repro from #31770
38+
39+
type KeyValue = [string, string?];
40+
let [key, value]: KeyValue = ["foo"];
41+
value.toUpperCase(); // Error
3642

3743

3844
//// [destructuringControlFlow.js]
@@ -69,3 +75,5 @@ function f4() {
6975
(x = 0["x"]); // Error
7076
(_a = "x" + "", x = 0[_a]); // Errpr
7177
}
78+
var _a = ["foo"], key = _a[0], value = _a[1];
79+
value.toUpperCase(); // Error

tests/baselines/reference/destructuringControlFlow.symbols

+13
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,16 @@ function f4() {
122122
>x : Symbol(x, Decl(destructuringControlFlow.ts, 29, 7))
123123
}
124124

125+
// Repro from #31770
126+
127+
type KeyValue = [string, string?];
128+
>KeyValue : Symbol(KeyValue, Decl(destructuringControlFlow.ts, 33, 1))
129+
130+
let [key, value]: KeyValue = ["foo"];
131+
>key : Symbol(key, Decl(destructuringControlFlow.ts, 38, 5))
132+
>value : Symbol(value, Decl(destructuringControlFlow.ts, 38, 9))
133+
>KeyValue : Symbol(KeyValue, Decl(destructuringControlFlow.ts, 33, 1))
134+
135+
value.toUpperCase(); // Error
136+
>value : Symbol(value, Decl(destructuringControlFlow.ts, 38, 9))
137+

tests/baselines/reference/destructuringControlFlow.types

+17
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,20 @@ function f4() {
158158
>0 : 0
159159
}
160160

161+
// Repro from #31770
162+
163+
type KeyValue = [string, string?];
164+
>KeyValue : [string, (string | undefined)?]
165+
166+
let [key, value]: KeyValue = ["foo"];
167+
>key : string
168+
>value : string | undefined
169+
>["foo"] : [string]
170+
>"foo" : "foo"
171+
172+
value.toUpperCase(); // Error
173+
>value.toUpperCase() : any
174+
>value.toUpperCase : any
175+
>value : undefined
176+
>toUpperCase : any
177+

tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts

+6
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ function f4() {
3434
({ ["x"]: x } = 0); // Error
3535
({ ["x" + ""]: x } = 0); // Errpr
3636
}
37+
38+
// Repro from #31770
39+
40+
type KeyValue = [string, string?];
41+
let [key, value]: KeyValue = ["foo"];
42+
value.toUpperCase(); // Error

0 commit comments

Comments
 (0)