Skip to content

Fix destructuring control flow analysis #29053

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 12 commits into from
Dec 19, 2018
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
357 changes: 170 additions & 187 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1636,14 +1636,6 @@
"category": "Error",
"code": 2458
},
"Type '{0}' has no property '{1}' and no string index signature.": {
"category": "Error",
"code": 2459
},
"Type '{0}' has no property '{1}'.": {
"category": "Error",
"code": 2460
},
"Type '{0}' is not an array type.": {
"category": "Error",
"code": 2461
Expand Down Expand Up @@ -1760,7 +1752,7 @@
"category": "Error",
"code": 2492
},
"Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'.": {
"Tuple type '{0}' of length '{1}' has no element at index '{2}'.": {
"category": "Error",
"code": 2493
},
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,9 @@ namespace ts {
export type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
export type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;

/* @internal */
export type AccessExpression = PropertyAccessExpression | ElementAccessExpression;

export interface PropertyAccessExpression extends MemberExpression, NamedDeclaration {
kind: SyntaxKind.PropertyAccessExpression;
expression: LeftHandSideExpression;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4590,6 +4590,10 @@ namespace ts {
|| kind === SyntaxKind.JSDocFunctionType
|| kind === SyntaxKind.JSDocVariadicType;
}

export function isAccessExpression(node: Node): node is AccessExpression {
return node.kind === SyntaxKind.PropertyAccessExpression || node.kind === SyntaxKind.ElementAccessExpression;
}
}

namespace ts {
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of31.errors.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2459: Type 'undefined' has no property 'a' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2459: Type 'undefined' has no property 'b' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,8): error TS2339: Property 'a' does not exist on type 'undefined'.
tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts(3,18): error TS2339: Property 'b' does not exist on type 'undefined'.


==== tests/cases/conformance/statements/for-ofStatements/ES5For-of31.ts (2 errors) ====
var a: string, b: number;

for ({ a: b = 1, b: a = ""} of []) {
~
!!! error TS2459: Type 'undefined' has no property 'a' and no string index signature.
!!! error TS2339: Property 'a' does not exist on type 'undefined'.
~
!!! error TS2459: Type 'undefined' has no property 'b' and no string index signature.
!!! error TS2339: Property 'b' does not exist on type 'undefined'.
a;
b;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,12): error TS2460: Type 'StrNum' has no property '2'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2493: Tuple type '[string, number]' of length '2' has no element at index '2'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,12): error TS2460: Type '{ 0: string; 1: number; length: 2; }' has no property '2'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2741: Property '2' is missing in type 'StrNum' but required in type '[number, number, number]'.
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, number, number]': 2, pop, push, concat, and 16 more.
Expand Down Expand Up @@ -30,7 +28,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(31,5): error
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error TS2740: Type '{ 0: string; 1: number; length: 2; }' is missing the following properties from type '[number, string]': pop, push, concat, join, and 15 more.


==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (17 errors) ====
interface StrNum extends Array<string|number> {
0: string;
1: number;
Expand All @@ -47,15 +45,11 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(32,5): error

var [a, b, c] = x;
~
!!! error TS2493: Tuple type '[string, number]' with length '2' cannot be assigned to tuple with length '3'.
!!! error TS2493: Tuple type '[string, number]' of length '2' has no element at index '2'.
var [d, e, f] = y;
~
!!! error TS2460: Type 'StrNum' has no property '2'.
var [g, h, i] = z;
~~~~~~~~~
!!! error TS2461: Type '{ 0: string; 1: number; length: 2; }' is not an array type.
~
!!! error TS2460: Type '{ 0: string; 1: number; length: 2; }' has no property '2'.
var j1: [number, number, number] = x;
~~
!!! error TS2741: Property '2' is missing in type '[string, number]' but required in type '[number, number, number]'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arityAndOrderCompatibility01.types
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ var z: {
var [a, b, c] = x;
>a : string
>b : number
>c : any
>c : undefined
>x : [string, number]

var [d, e, f] = y;
>d : string
>e : number
>f : any
>f : string | number
>y : StrNum

var [g, h, i] = z;
>g : string
>h : number
>g : any
>h : any
>i : any
>z : { 0: string; 1: number; length: 2; }

Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/bestCommonTypeOfTuple.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(22,13): error TS2339: Property '2' does not exist on type '[(x: number) => string, (x: number) => number]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(23,13): error TS2339: Property '2' does not exist on type '[E1, E2]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(24,13): error TS2339: Property '2' does not exist on type '[number, any]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(25,13): error TS2339: Property '3' does not exist on type '[E1, E2, number]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(22,13): error TS2493: Tuple type '[(x: number) => string, (x: number) => number]' of length '2' has no element at index '2'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(23,13): error TS2493: Tuple type '[E1, E2]' of length '2' has no element at index '2'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(24,13): error TS2493: Tuple type '[number, any]' of length '2' has no element at index '2'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts(25,13): error TS2493: Tuple type '[E1, E2, number]' of length '3' has no element at index '3'.


==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple.ts (4 errors) ====
Expand All @@ -28,13 +28,13 @@ tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfT
t4 = [E1.one, E2.two, 20];
var e1 = t1[2]; // {}
~
!!! error TS2339: Property '2' does not exist on type '[(x: number) => string, (x: number) => number]'.
!!! error TS2493: Tuple type '[(x: number) => string, (x: number) => number]' of length '2' has no element at index '2'.
var e2 = t2[2]; // {}
~
!!! error TS2339: Property '2' does not exist on type '[E1, E2]'.
!!! error TS2493: Tuple type '[E1, E2]' of length '2' has no element at index '2'.
var e3 = t3[2]; // any
~
!!! error TS2339: Property '2' does not exist on type '[number, any]'.
!!! error TS2493: Tuple type '[number, any]' of length '2' has no element at index '2'.
var e4 = t4[3]; // number
~
!!! error TS2339: Property '3' does not exist on type '[E1, E2, number]'.
!!! error TS2493: Tuple type '[E1, E2, number]' of length '3' has no element at index '3'.
20 changes: 10 additions & 10 deletions tests/baselines/reference/bestCommonTypeOfTuple2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(17,14): error TS2339: Property '4' does not exist on type '[C, base]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(18,14): error TS2339: Property '4' does not exist on type '[C, D]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(19,14): error TS2339: Property '4' does not exist on type '[C1, D1]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(20,14): error TS2339: Property '2' does not exist on type '[base1, C1]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(21,14): error TS2339: Property '2' does not exist on type '[C1, F]'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(17,14): error TS2493: Tuple type '[C, base]' of length '2' has no element at index '4'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(18,14): error TS2493: Tuple type '[C, D]' of length '2' has no element at index '4'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(19,14): error TS2493: Tuple type '[C1, D1]' of length '2' has no element at index '4'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(20,14): error TS2493: Tuple type '[base1, C1]' of length '2' has no element at index '2'.
tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts(21,14): error TS2493: Tuple type '[C1, F]' of length '2' has no element at index '2'.


==== tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfTuple2.ts (5 errors) ====
Expand All @@ -24,17 +24,17 @@ tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfT

var e11 = t1[4]; // base
~
!!! error TS2339: Property '4' does not exist on type '[C, base]'.
!!! error TS2493: Tuple type '[C, base]' of length '2' has no element at index '4'.
var e21 = t2[4]; // {}
~
!!! error TS2339: Property '4' does not exist on type '[C, D]'.
!!! error TS2493: Tuple type '[C, D]' of length '2' has no element at index '4'.
var e31 = t3[4]; // C1
~
!!! error TS2339: Property '4' does not exist on type '[C1, D1]'.
!!! error TS2493: Tuple type '[C1, D1]' of length '2' has no element at index '4'.
var e41 = t4[2]; // base1
~
!!! error TS2339: Property '2' does not exist on type '[base1, C1]'.
!!! error TS2493: Tuple type '[base1, C1]' of length '2' has no element at index '2'.
var e51 = t5[2]; // {}
~
!!! error TS2339: Property '2' does not exist on type '[C1, F]'.
!!! error TS2493: Tuple type '[C1, F]' of length '2' has no element at index '2'.

4 changes: 2 additions & 2 deletions tests/baselines/reference/castingTuple.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests/cases/conformance/types/tuple/castingTuple.ts(14,15): error TS2352: Conver
tests/cases/conformance/types/tuple/castingTuple.ts(15,14): error TS2352: Conversion of type '[number, string]' to type '[number, string, boolean]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
tests/cases/conformance/types/tuple/castingTuple.ts(18,21): error TS2352: Conversion of type '[C, D]' to type '[C, D, A]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Property '2' is missing in type '[C, D]' but required in type '[C, D, A]'.
tests/cases/conformance/types/tuple/castingTuple.ts(20,33): error TS2339: Property '5' does not exist on type '[C, D, A]'.
tests/cases/conformance/types/tuple/castingTuple.ts(20,33): error TS2493: Tuple type '[C, D, A]' of length '3' has no element at index '5'.
tests/cases/conformance/types/tuple/castingTuple.ts(30,10): error TS2352: Conversion of type '[number, string]' to type '[number, number]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'string' is not comparable to type 'number'.
tests/cases/conformance/types/tuple/castingTuple.ts(31,10): error TS2352: Conversion of type '[C, D]' to type '[A, I]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Expand Down Expand Up @@ -49,7 +49,7 @@ tests/cases/conformance/types/tuple/castingTuple.ts(33,1): error TS2304: Cannot
var eleFromCDA1 = classCDATuple[2]; // A
var eleFromCDA2 = classCDATuple[5]; // C | D | A
~
!!! error TS2339: Property '5' does not exist on type '[C, D, A]'.
!!! error TS2493: Tuple type '[C, D, A]' of length '3' has no element at index '5'.
var t10: [E1, E2] = [E1.one, E2.one];
var t11 = <[number, number]>t10;
var array1 = <{}[]>emptyObjTuple;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ tests/cases/compiler/computedPropertiesInDestructuring1.ts(20,8): error TS2349:
tests/cases/compiler/computedPropertiesInDestructuring1.ts(20,8): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(21,8): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(21,12): error TS2339: Property 'toExponential' does not exist on type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(24,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(28,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(30,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(31,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(33,4): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(33,4): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,4): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,5): error TS2365: Operator '+' cannot be applied to types '1' and '{}'.


==== tests/cases/compiler/computedPropertiesInDestructuring1.ts (14 errors) ====
==== tests/cases/compiler/computedPropertiesInDestructuring1.ts (20 errors) ====
// destructuring in variable declarations
let foo = "bar";
let {[foo]: bar} = {bar: "bar"};
Expand Down Expand Up @@ -63,18 +69,30 @@ tests/cases/compiler/computedPropertiesInDestructuring1.ts(34,5): error TS2365:

// destructuring assignment
({[foo]: bar} = {bar: "bar"});
~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.

({["bar"]: bar2} = {bar: "bar"});

({[foo2()]: bar3} = {bar: "bar"});
~~~~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.

[{[foo]: bar4}] = [{bar: "bar"}];
~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
[{[foo2()]: bar5}] = [{bar: "bar"}];
~~~~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.

[{[foo()]: bar4}] = [{bar: "bar"}];
~~~~~
!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
~~~~~
!!! error TS2538: Type 'any' cannot be used as an index type.
[{[(1 + {})]: bar4}] = [{bar: "bar"}];
~~~~~~~~
!!! error TS2538: Type 'any' cannot be used as an index type.
~~~~~~
!!! error TS2365: Operator '+' cannot be applied to types '1' and '{}'.

Expand Down
Loading