Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
349 changes: 163 additions & 186 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

12 changes: 0 additions & 12 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,10 +1752,6 @@
"category": "Error",
"code": 2492
},
"Tuple type '{0}' with length '{1}' cannot be assigned to tuple with length '{2}'.": {
"category": "Error",
"code": 2493
},
"Using a string in a 'for...of' statement is only supported in ECMAScript 5 and higher.": {
"category": "Error",
"code": 2494
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,8 @@ namespace ts {
export type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
export type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;

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 @@ -5586,6 +5586,10 @@ namespace ts {
return node.kind === SyntaxKind.ElementAccessExpression;
}

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

export function isCallExpression(node: Node): node is CallExpression {
return node.kind === SyntaxKind.CallExpression;
}
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;
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ declare namespace ts {
}
type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;
type AccessExpression = PropertyAccessExpression | ElementAccessExpression;
interface PropertyAccessExpression extends MemberExpression, NamedDeclaration {
kind: SyntaxKind.PropertyAccessExpression;
expression: LeftHandSideExpression;
Expand Down Expand Up @@ -3350,6 +3351,7 @@ declare namespace ts {
function isObjectLiteralExpression(node: Node): node is ObjectLiteralExpression;
function isPropertyAccessExpression(node: Node): node is PropertyAccessExpression;
function isElementAccessExpression(node: Node): node is ElementAccessExpression;
function isAccessExpression(node: Node): node is AccessExpression;
function isCallExpression(node: Node): node is CallExpression;
function isNewExpression(node: Node): node is NewExpression;
function isTaggedTemplateExpression(node: Node): node is TaggedTemplateExpression;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ declare namespace ts {
}
type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression;
type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression;
type AccessExpression = PropertyAccessExpression | ElementAccessExpression;
interface PropertyAccessExpression extends MemberExpression, NamedDeclaration {
kind: SyntaxKind.PropertyAccessExpression;
expression: LeftHandSideExpression;
Expand Down Expand Up @@ -3350,6 +3351,7 @@ declare namespace ts {
function isObjectLiteralExpression(node: Node): node is ObjectLiteralExpression;
function isPropertyAccessExpression(node: Node): node is PropertyAccessExpression;
function isElementAccessExpression(node: Node): node is ElementAccessExpression;
function isAccessExpression(node: Node): node is AccessExpression;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to export this from the public API?

function isCallExpression(node: Node): node is CallExpression;
function isNewExpression(node: Node): node is NewExpression;
function isTaggedTemplateExpression(node: Node): node is TaggedTemplateExpression;
Expand Down
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 TS2339: Property '2' does not exist on type '[string, number]'.
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 TS2339: Property '2' does not exist on type '[string, number]'.
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(21,8): error TS23
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(21,8): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(22,8): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(22,12): error TS2339: Property 'toExponential' does not exist on type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(25,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(29,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(31,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(32,4): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(34,4): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(34,4): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,4): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,5): error TS2365: Operator '+' cannot be applied to types '1' and '{}'.


==== tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts (14 errors) ====
==== tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts (20 errors) ====
// destructuring in variable declarations
let foo = "bar";
let {[foo]: bar} = {bar: "bar"};
Expand Down Expand Up @@ -64,18 +70,30 @@ tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(35,5): error TS23

// 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 '{}'.

4 changes: 2 additions & 2 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ function f9() {
>{} : {}

var [c, d] = { 0: 10, 1: 20 }; // Error, not array type
>c : number
>d : number
>c : any
>d : any
>{ 0: 10, 1: 20 } : { 0: number; 1: number; }
>0 : number
>10 : 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var [b3 = "string", b4, b5] = bar(); // Error
>b3 : string | Number
>"string" : "string"
>b4 : Number
>b5 : Number
>b5 : number
>bar() : J
>bar : () => J

Expand Down
47 changes: 47 additions & 0 deletions tests/baselines/reference/destructuringControlFlow.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 (3 errors) ====
function f1(obj: { a?: string }) {
if (obj.a) {
obj = {};
let a1 = obj["a"]; // string | undefined
let a2 = obj.a; // string | undefined
}
}

function f2(obj: [number, string] | null[]) {
let a0 = obj[0]; // number | null
let a1 = obj[1]; // string | null
let [b0, b1] = obj;
([a0, a1] = obj);
if (obj[0] && obj[1]) {
let c0 = obj[0]; // number
let c1 = obj[1]; // string
let [d0, d1] = obj;
([c0, c1] = obj);
}
}

function f3(obj: { a?: number, b?: string }) {
if (obj.a && obj.b) {
let { a, b } = obj; // number, string
({ a, b } = obj);
}
}

function f4() {
let x: boolean;
({ x } = 0); // Error
~
!!! error TS2339: Property 'x' does not exist on type 'Number'.
({ ["x"]: x } = 0); // Error
~~~
!!! error TS2339: Property 'x' does not exist on type 'Number'.
({ ["x" + ""]: x } = 0); // Errpr
~~~~~~~~
!!! error TS2537: Type 'Number' has no matching index signature for type 'string'.
}

71 changes: 71 additions & 0 deletions tests/baselines/reference/destructuringControlFlow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//// [destructuringControlFlow.ts]
function f1(obj: { a?: string }) {
if (obj.a) {
obj = {};
let a1 = obj["a"]; // string | undefined
let a2 = obj.a; // string | undefined
}
}

function f2(obj: [number, string] | null[]) {
let a0 = obj[0]; // number | null
let a1 = obj[1]; // string | null
let [b0, b1] = obj;
([a0, a1] = obj);
if (obj[0] && obj[1]) {
let c0 = obj[0]; // number
let c1 = obj[1]; // string
let [d0, d1] = obj;
([c0, c1] = obj);
}
}

function f3(obj: { a?: number, b?: string }) {
if (obj.a && obj.b) {
let { a, b } = obj; // number, string
({ a, b } = obj);
}
}

function f4() {
let x: boolean;
({ x } = 0); // Error
({ ["x"]: x } = 0); // Error
({ ["x" + ""]: x } = 0); // Errpr
}


//// [destructuringControlFlow.js]
"use strict";
function f1(obj) {
if (obj.a) {
obj = {};
var a1 = obj["a"]; // string | undefined
var a2 = obj.a; // string | undefined
}
}
function f2(obj) {
var a0 = obj[0]; // number | null
var a1 = obj[1]; // string | null
var b0 = obj[0], b1 = obj[1];
(a0 = obj[0], a1 = obj[1]);
if (obj[0] && obj[1]) {
var c0 = obj[0]; // number
var c1 = obj[1]; // string
var d0 = obj[0], d1 = obj[1];
(c0 = obj[0], c1 = obj[1]);
}
}
function f3(obj) {
if (obj.a && obj.b) {
var a = obj.a, b = obj.b; // number, string
(a = obj.a, b = obj.b);
}
}
function f4() {
var _a;
var x;
(x = 0..x); // Error
(x = 0["x"]); // Error
(_a = "x" + "", x = 0[_a]); // Errpr
}
Loading