Skip to content
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

Check destructuring validity the same way element accesses and indexed accesses are checked #24700

Merged
merged 6 commits into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
109 changes: 43 additions & 66 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of27.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-of27.ts(1,11): error TS2459: Type 'number' has no property 'x' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of27.ts(1,21): error TS2459: Type 'number' has no property 'y' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of27.ts(1,11): error TS2339: Property 'x' does not exist on type 'number'.
tests/cases/conformance/statements/for-ofStatements/ES5For-of27.ts(1,21): error TS2339: Property 'y' does not exist on type 'number'.


==== tests/cases/conformance/statements/for-ofStatements/ES5For-of27.ts (2 errors) ====
for (var {x: a = 0, y: b = 1} of [2, 3]) {
~
!!! error TS2459: Type 'number' has no property 'x' and no string index signature.
!!! error TS2339: Property 'x' does not exist on type 'number'.
~
!!! error TS2459: Type 'number' has no property 'y' and no string index signature.
!!! error TS2339: Property 'y' does not exist on type 'number'.
a;
b;
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of29.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-of29.ts(1,13): error TS2459: Type 'number' has no property 'x' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of29.ts(1,23): error TS2459: Type 'number' has no property 'y' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of29.ts(1,13): error TS2339: Property 'x' does not exist on type 'number'.
tests/cases/conformance/statements/for-ofStatements/ES5For-of29.ts(1,23): error TS2339: Property 'y' does not exist on type 'number'.


==== tests/cases/conformance/statements/for-ofStatements/ES5For-of29.ts (2 errors) ====
for (const {x: a = 0, y: b = 1} of [2, 3]) {
~
!!! error TS2459: Type 'number' has no property 'x' and no string index signature.
!!! error TS2339: Property 'x' does not exist on type 'number'.
~
!!! error TS2459: Type 'number' has no property 'y' and no string index signature.
!!! error TS2339: Property 'y' does not exist on type 'number'.
a;
b;
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of35.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
tests/cases/conformance/statements/for-ofStatements/ES5For-of35.ts(1,13): error TS2459: Type 'number' has no property 'x' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of35.ts(1,23): error TS2459: Type 'number' has no property 'y' and no string index signature.
tests/cases/conformance/statements/for-ofStatements/ES5For-of35.ts(1,13): error TS2339: Property 'x' does not exist on type 'number'.
tests/cases/conformance/statements/for-ofStatements/ES5For-of35.ts(1,23): error TS2339: Property 'y' does not exist on type 'number'.


==== tests/cases/conformance/statements/for-ofStatements/ES5For-of35.ts (2 errors) ====
for (const {x: a = 0, y: b = 1} of [2, 3]) {
~
!!! error TS2459: Type 'number' has no property 'x' and no string index signature.
!!! error TS2339: Property 'x' does not exist on type 'number'.
~
!!! error TS2459: Type 'number' has no property 'y' and no string index signature.
!!! error TS2339: Property 'y' does not exist on type 'number'.
a;
b;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(2,12): error TS2448: Block-scoped variable 'a' used before its declaration.
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(2,12): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(5,12): error TS2448: Block-scoped variable 'a' used before its declaration.
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(5,12): error TS2538: Type 'any' cannot be used as an index type.
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(8,7): error TS2448: Block-scoped variable 'b' used before its declaration.
tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts(8,7): error TS2538: Type 'any' cannot be used as an index type.


==== tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts (3 errors) ====
==== tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts (6 errors) ====
// 1:
for (let {[a]: a} of [{ }]) continue;
~
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
!!! related TS2728 tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts:2:16: 'a' is declared here.
~
!!! error TS2538: Type 'any' cannot be used as an index type.
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't feel like a legal error, but it seems like we're only issuing it when there's already an error on the same node. Is there an issue with the error type leaking out into an error message?

Copy link
Member Author

Choose a reason for hiding this comment

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

Most certainly. It's because we choose to consider indexing with any an error today (esp. if the thing being indexed has no index signatures), but destructuring accesses don't flag that right now. This PR aligns them.


// 2:
for (let {[a]: a} = { }; false; ) continue;
~
!!! error TS2448: Block-scoped variable 'a' used before its declaration.
!!! related TS2728 tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts:5:16: 'a' is declared here.
~
!!! error TS2538: Type 'any' cannot be used as an index type.

// 3:
let {[b]: b} = { };
~
!!! error TS2448: Block-scoped variable 'b' used before its declaration.
!!! related TS2728 tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts:8:11: 'b' is declared here.
!!! related TS2728 tests/cases/compiler/blockScopedBindingUsedBeforeDef.ts:8:11: 'b' is declared here.
~
!!! error TS2538: Type 'any' cannot be used as an index type.
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
tests/cases/compiler/computedPropertiesInDestructuring1.ts(3,7): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(8,7): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(10,8): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(11,8): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(14,15): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(15,15): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(16,16): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(17,16): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1.ts(20,8): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'String' has no compatible call signatures.
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(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(34,5): error TS2365: Operator '+' cannot be applied to types '1' and '{}'.


==== tests/cases/compiler/computedPropertiesInDestructuring1.ts (4 errors) ====
==== tests/cases/compiler/computedPropertiesInDestructuring1.ts (14 errors) ====
// destructuring in variable declarations
let foo = "bar";
let {[foo]: bar} = {bar: "bar"};
~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.

let {["bar"]: bar2} = {bar: "bar"};

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

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

function f1({["bar"]: x}: { bar: number }) {}
function f2({[foo]: x}: { bar: number }) {}
~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f3({[foo2()]: x}: { bar: number }) {}
~~~~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f4([{[foo]: x}]: [{ bar: number }]) {}
~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f5([{[foo2()]: x}]: [{ bar: number }]) {}
~~~~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.

// report errors on type errors in computed properties used in destructuring
let [{[foo()]: bar6}] = [{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.
let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
~~~~~~~~~~~~~~~~~~~
!!! error TS2538: Type 'any' cannot be used as an index type.
~~~~~~~~~~~~~
!!! error TS2339: Property 'toExponential' does not exist on type 'string'.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(3,7): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(9,7): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(11,8): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(12,8): error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(15,15): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(16,15): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(17,16): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(18,16): error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts(21,8): 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(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(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(35,5): error TS2365: Operator '+' cannot be applied to types '1' and '{}'.


==== tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts (4 errors) ====
==== tests/cases/compiler/computedPropertiesInDestructuring1_ES6.ts (14 errors) ====
// destructuring in variable declarations
let foo = "bar";
let {[foo]: bar} = {bar: "bar"};
~~~
!!! error TS2537: Type '{ bar: string; }' has no matching index signature for type 'string'.

let {["bar"]: bar2} = {bar: "bar"};
let {[11]: bar2_1} = {11: "bar"};

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

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

function f1({["bar"]: x}: { bar: number }) {}
function f2({[foo]: x}: { bar: number }) {}
~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f3({[foo2()]: x}: { bar: number }) {}
~~~~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f4([{[foo]: x}]: [{ bar: number }]) {}
~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.
function f5([{[foo2()]: x}]: [{ bar: number }]) {}
~~~~~~
!!! error TS2537: Type '{ bar: number; }' has no matching index signature for type 'string'.

// report errors on type errors in computed properties used in destructuring
let [{[foo()]: bar6}] = [{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.
let [{[foo.toExponential()]: bar7}] = [{bar: "bar"}];
~~~~~~~~~~~~~~~~~~~
!!! error TS2538: Type 'any' cannot be used as an index type.
~~~~~~~~~~~~~
!!! error TS2339: Property 'toExponential' does not exist on type 'string'.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests/cases/compiler/computedPropertiesInDestructuring2.ts(2,7): error TS2537: Type '{}' has no matching index signature for type 'string'.


==== tests/cases/compiler/computedPropertiesInDestructuring2.ts (1 errors) ====
let foo2 = () => "bar";
let {[foo2()]: bar3} = {};
~~~~~~
!!! error TS2537: Type '{}' has no matching index signature for type 'string'.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests/cases/compiler/computedPropertiesInDestructuring2_ES6.ts(2,7): error TS2537: Type '{}' has no matching index signature for type 'string'.


==== tests/cases/compiler/computedPropertiesInDestructuring2_ES6.ts (1 errors) ====
let foo2 = () => "bar";
let {[foo2()]: bar3} = {};
~~~~~~
!!! error TS2537: Type '{}' has no matching index signature for type 'string'.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(67,9): e
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(68,9): error TS2461: Type '{ 0: number; 1: number; }' is not an array type.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,14): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,11): error TS2459: Type 'undefined[]' has no property 'a' and no string index signature.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,14): error TS2459: Type 'undefined[]' has no property 'b' and no string index signature.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,11): error TS2339: Property 'a' does not exist on type 'undefined[]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,14): error TS2339: Property 'b' does not exist on type 'undefined[]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(106,17): error TS2322: Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
Property 'x' is missing in type '{ y: boolean; }'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6): error TS2322: Type 'string' is not assignable to type 'number'.
Expand Down Expand Up @@ -133,9 +133,9 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
var { a, b } = []; // Error
~
!!! 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[]'.
}

function f11() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class C<T extends Options> {
>method : () => void

let { a, b } = this.foo;
>a : T["a"]
>b : T["b"]
>a : { [P in keyof T]: T[P]; }["a"]
>b : { [P in keyof T]: T[P]; }["b"]
>this.foo : { [P in keyof T]: T[P]; }
>this : this
>foo : { [P in keyof T]: T[P]; }

!(a && b);
>!(a && b) : false
>(a && b) : T["b"]
>a && b : T["b"]
>a : T["a"]
>b : T["b"]
>(a && b) : { [P in keyof T]: T[P]; }["b"]
>a && b : { [P in keyof T]: T[P]; }["b"]
>a : { [P in keyof T]: T[P]; }["a"]
>b : { [P in keyof T]: T[P]; }["b"]

a;
>a : T["a"]
>a : { [P in keyof T]: T[P]; }["a"]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts(11,7): error TS2459: Type '{ prop: string; }' has no property '[notPresent]' and no string index signature.
tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts(11,8): error TS2339: Property 'prop2' does not exist on type '{ prop: string; }'.


==== tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts (1 errors) ====
Expand All @@ -13,6 +13,6 @@ tests/cases/compiler/destructuredLateBoundNameHasCorrectTypes.ts(11,7): error TS
const notPresent = "prop2";

let { [notPresent]: computed2 } = { prop: "b" };
~~~~~~~~~~~~
!!! error TS2459: Type '{ prop: string; }' has no property '[notPresent]' and no string index signature.
~~~~~~~~~~
!!! error TS2339: Property 'prop2' does not exist on type '{ prop: string; }'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [destructuredMaappedTypeIsNotImplicitlyAny.ts]
function foo<T extends string>(key: T, obj: { [_ in T]: number }) {
const { [key]: bar } = obj; // Element implicitly has an 'any' type because type '{ [_ in T]: number; }' has no index signature.
bar; // bar : any

// Note: this does work:
const lorem = obj[key];
}

//// [destructuredMaappedTypeIsNotImplicitlyAny.js]
function foo(key, obj) {
var _a = key, bar = obj[_a]; // Element implicitly has an 'any' type because type '{ [_ in T]: number; }' has no index signature.
bar; // bar : any
// Note: this does work:
var lorem = obj[key];
}
Loading