Skip to content

Fix tuple destructuring control flow analysis #32035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14440,7 +14440,7 @@ namespace ts {
if (propType) {
return propType;
}
if (everyType(type, isTupleType) && !everyType(type, t => !(<TupleTypeReference>t).target.hasRestElement)) {
if (everyType(type, isTupleType)) {
return mapType(type, t => getRestTypeOfTupleType(<TupleTypeReference>t) || undefinedType);
}
return undefined;
Expand Down
11 changes: 10 additions & 1 deletion tests/baselines/reference/destructuringControlFlow.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(31,8): error TS2339: Property 'x' does not exist on type 'Number'.
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(32,9): error TS2339: Property 'x' does not exist on type 'Number'.
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(33,9): error TS2537: Type 'Number' has no matching index signature for type 'string'.
tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(40,1): error TS2532: Object is possibly 'undefined'.


==== tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts (3 errors) ====
==== tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts (4 errors) ====
function f1(obj: { a?: string }) {
if (obj.a) {
obj = {};
Expand Down Expand Up @@ -44,4 +45,12 @@ tests/cases/conformance/es6/destructuring/destructuringControlFlow.ts(33,9): err
~~~~~~~~
!!! error TS2537: Type 'Number' has no matching index signature for type 'string'.
}

// Repro from #31770

type KeyValue = [string, string?];
let [key, value]: KeyValue = ["foo"];
value.toUpperCase(); // Error
~~~~~
!!! error TS2532: Object is possibly 'undefined'.

8 changes: 8 additions & 0 deletions tests/baselines/reference/destructuringControlFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ function f4() {
({ ["x"]: x } = 0); // Error
({ ["x" + ""]: x } = 0); // Errpr
}

// Repro from #31770

type KeyValue = [string, string?];
let [key, value]: KeyValue = ["foo"];
value.toUpperCase(); // Error


//// [destructuringControlFlow.js]
Expand Down Expand Up @@ -69,3 +75,5 @@ function f4() {
(x = 0["x"]); // Error
(_a = "x" + "", x = 0[_a]); // Errpr
}
var _a = ["foo"], key = _a[0], value = _a[1];
value.toUpperCase(); // Error
13 changes: 13 additions & 0 deletions tests/baselines/reference/destructuringControlFlow.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ function f4() {
>x : Symbol(x, Decl(destructuringControlFlow.ts, 29, 7))
}

// Repro from #31770

type KeyValue = [string, string?];
>KeyValue : Symbol(KeyValue, Decl(destructuringControlFlow.ts, 33, 1))

let [key, value]: KeyValue = ["foo"];
>key : Symbol(key, Decl(destructuringControlFlow.ts, 38, 5))
>value : Symbol(value, Decl(destructuringControlFlow.ts, 38, 9))
>KeyValue : Symbol(KeyValue, Decl(destructuringControlFlow.ts, 33, 1))

value.toUpperCase(); // Error
>value : Symbol(value, Decl(destructuringControlFlow.ts, 38, 9))

17 changes: 17 additions & 0 deletions tests/baselines/reference/destructuringControlFlow.types
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,20 @@ function f4() {
>0 : 0
}

// Repro from #31770

type KeyValue = [string, string?];
>KeyValue : [string, (string | undefined)?]

let [key, value]: KeyValue = ["foo"];
>key : string
>value : string | undefined
>["foo"] : [string]
>"foo" : "foo"

value.toUpperCase(); // Error
>value.toUpperCase() : any
>value.toUpperCase : any
>value : undefined
>toUpperCase : any

Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ function f4() {
({ ["x"]: x } = 0); // Error
({ ["x" + ""]: x } = 0); // Errpr
}

// Repro from #31770

type KeyValue = [string, string?];
let [key, value]: KeyValue = ["foo"];
value.toUpperCase(); // Error