-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
weswigham
merged 6 commits into
microsoft:master
from
weswigham:generic-mapped-destructuring
Nov 1, 2018
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26b3665
Check destructuring validity the same way element accesses and indexe…
weswigham e2b492a
Merge branch 'master' into generic-mapped-destructuring
weswigham 5b900c7
Merge branch 'master' into generic-mapped-destructuring
weswigham d933812
Accept updated test baseline
weswigham 8250384
Use raw apparent type instead of passing in flag to sometimes make one
weswigham 96ad54c
Use `checkComputedPropertyName`
weswigham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
13 changes: 11 additions & 2 deletions
13
tests/baselines/reference/blockScopedBindingUsedBeforeDef.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
// 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. |
32 changes: 31 additions & 1 deletion
32
tests/baselines/reference/computedPropertiesInDestructuring1.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 31 additions & 1 deletion
32
tests/baselines/reference/computedPropertiesInDestructuring1_ES6.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/computedPropertiesInDestructuring2.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. |
8 changes: 8 additions & 0 deletions
8
tests/baselines/reference/computedPropertiesInDestructuring2_ES6.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/destructuredMaappedTypeIsNotImplicitlyAny.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.