-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Better errors for indexing gettable/settable values #26446
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
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e49b841
give suggestions when index signature given
collin5 b66fea6
add tests for noImplicitAny indexing on Object
collin5 a481be7
remove comments regarding error messages
collin5 5256ff7
recommend set if el is on RHS of assignment else get
collin5 cab6e04
add new baseline tests
collin5 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
add tests for noImplicitAny indexing on Object
- Loading branch information
commit b66fea6e4c9fdf608311305daff0a531fc5a7d05
There are no files selected for viewing
35 changes: 32 additions & 3 deletions
35
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.errors.txt
This file contains hidden or 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,8 +1,37 @@ | ||
| tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(1,9): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature. | ||
| tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(8,1): error TS7043: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'c.get' ? | ||
| tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(14,1): error TS7043: Element implicitly has an 'any' type because type '{ set: (key: string) => string; }' has no index signature. Did you mean to call 'd.set' ? | ||
| tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(21,1): error TS7043: Element implicitly has an 'any' type because type '{ set: (key: string) => string; get: (key: string) => string; }' has no index signature. Did you mean to call 'e.get or e.set' ? | ||
|
|
||
|
|
||
| ==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (1 errors) ==== | ||
| var x = {}["hello"]; | ||
| ==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (4 errors) ==== | ||
| var a = {}["hello"]; | ||
| ~~~~~~~~~~~ | ||
| !!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature. | ||
| var y: string = { '': 'foo' }['']; | ||
| var b: string = { '': 'foo' }['']; | ||
|
|
||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| c['hello']; | ||
| ~~~~~~~~~~ | ||
| !!! error TS7043: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'c.get' ? | ||
|
|
||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| set: (key: string) => 'foobar' | ||
| }; | ||
| d['hello']; | ||
| ~~~~~~~~~~ | ||
| !!! error TS7043: Element implicitly has an 'any' type because type '{ set: (key: string) => string; }' has no index signature. Did you mean to call 'd.set' ? | ||
|
|
||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| set: (key: string) => 'foobar', | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| e['hello']; | ||
| ~~~~~~~~~~ | ||
| !!! error TS7043: Element implicitly has an 'any' type because type '{ set: (key: string) => string; get: (key: string) => string; }' has no index signature. Did you mean to call 'e.get or e.set' ? | ||
|
|
44 changes: 40 additions & 4 deletions
44
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.js
This file contains hidden or 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,7 +1,43 @@ | ||
| //// [noImplicitAnyStringIndexerOnObject.ts] | ||
| var x = {}["hello"]; | ||
| var y: string = { '': 'foo' }['']; | ||
| var a = {}["hello"]; | ||
| var b: string = { '': 'foo' }['']; | ||
|
|
||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| c['hello']; | ||
|
|
||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| set: (key: string) => 'foobar' | ||
| }; | ||
| d['hello']; | ||
|
|
||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| set: (key: string) => 'foobar', | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| e['hello']; | ||
|
|
||
|
|
||
| //// [noImplicitAnyStringIndexerOnObject.js] | ||
| var x = {}["hello"]; | ||
| var y = { '': 'foo' }['']; | ||
| var a = {}["hello"]; | ||
| var b = { '': 'foo' }['']; | ||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| get: function (key) { return 'foobar'; } | ||
| }; | ||
| c['hello']; | ||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| set: function (key) { return 'foobar'; } | ||
| }; | ||
| d['hello']; | ||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| set: function (key) { return 'foobar'; }, | ||
| get: function (key) { return 'foobar'; } | ||
| }; | ||
| e['hello']; | ||
48 changes: 44 additions & 4 deletions
48
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols
This file contains hidden or 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,9 +1,49 @@ | ||
| === tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts === | ||
| var x = {}["hello"]; | ||
| >x : Symbol(x, Decl(noImplicitAnyStringIndexerOnObject.ts, 0, 3)) | ||
| var a = {}["hello"]; | ||
| >a : Symbol(a, Decl(noImplicitAnyStringIndexerOnObject.ts, 0, 3)) | ||
|
|
||
| var y: string = { '': 'foo' }['']; | ||
| >y : Symbol(y, Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 3)) | ||
| var b: string = { '': 'foo' }['']; | ||
| >b : Symbol(b, Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 3)) | ||
| >'' : Symbol('', Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 17)) | ||
| >'' : Symbol('', Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 17)) | ||
|
|
||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| >c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 3)) | ||
|
|
||
| get: (key: string) => 'foobar' | ||
| >get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 9)) | ||
| >key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 5, 8)) | ||
|
|
||
| }; | ||
| c['hello']; | ||
| >c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 3)) | ||
|
|
||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| >d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 3)) | ||
|
|
||
| set: (key: string) => 'foobar' | ||
| >set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 9)) | ||
| >key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 11, 8)) | ||
|
|
||
| }; | ||
| d['hello']; | ||
| >d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 3)) | ||
|
|
||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| >e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 3)) | ||
|
|
||
| set: (key: string) => 'foobar', | ||
| >set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 9)) | ||
| >key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 17, 8)) | ||
|
|
||
| get: (key: string) => 'foobar' | ||
| >get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 17, 33)) | ||
| >key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 18, 8)) | ||
|
|
||
| }; | ||
| e['hello']; | ||
| >e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 3)) | ||
|
|
65 changes: 61 additions & 4 deletions
65
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types
This file contains hidden or 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,15 +1,72 @@ | ||
| === tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts === | ||
| var x = {}["hello"]; | ||
| >x : any | ||
| var a = {}["hello"]; | ||
| >a : any | ||
| >{}["hello"] : any | ||
| >{} : {} | ||
| >"hello" : "hello" | ||
|
|
||
| var y: string = { '': 'foo' }['']; | ||
| >y : string | ||
| var b: string = { '': 'foo' }['']; | ||
| >b : string | ||
| >{ '': 'foo' }[''] : string | ||
| >{ '': 'foo' } : { '': string; } | ||
| >'' : string | ||
| >'foo' : "foo" | ||
| >'' : "" | ||
|
|
||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| >c : { get: (key: string) => string; } | ||
| >{ get: (key: string) => 'foobar'} : { get: (key: string) => string; } | ||
|
|
||
| get: (key: string) => 'foobar' | ||
| >get : (key: string) => string | ||
| >(key: string) => 'foobar' : (key: string) => string | ||
| >key : string | ||
| >'foobar' : "foobar" | ||
|
|
||
| }; | ||
| c['hello']; | ||
| >c['hello'] : any | ||
| >c : { get: (key: string) => string; } | ||
| >'hello' : "hello" | ||
|
|
||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| >d : { set: (key: string) => string; } | ||
| >{ set: (key: string) => 'foobar'} : { set: (key: string) => string; } | ||
|
|
||
| set: (key: string) => 'foobar' | ||
| >set : (key: string) => string | ||
| >(key: string) => 'foobar' : (key: string) => string | ||
| >key : string | ||
| >'foobar' : "foobar" | ||
|
|
||
| }; | ||
| d['hello']; | ||
| >d['hello'] : any | ||
| >d : { set: (key: string) => string; } | ||
| >'hello' : "hello" | ||
|
|
||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| >e : { set: (key: string) => string; get: (key: string) => string; } | ||
| >{ set: (key: string) => 'foobar', get: (key: string) => 'foobar'} : { set: (key: string) => string; get: (key: string) => string; } | ||
|
|
||
| set: (key: string) => 'foobar', | ||
| >set : (key: string) => string | ||
| >(key: string) => 'foobar' : (key: string) => string | ||
| >key : string | ||
| >'foobar' : "foobar" | ||
|
|
||
| get: (key: string) => 'foobar' | ||
| >get : (key: string) => string | ||
| >(key: string) => 'foobar' : (key: string) => string | ||
| >key : string | ||
| >'foobar' : "foobar" | ||
|
|
||
| }; | ||
| e['hello']; | ||
| >e['hello'] : any | ||
| >e : { set: (key: string) => string; get: (key: string) => string; } | ||
| >'hello' : "hello" | ||
|
|
23 changes: 21 additions & 2 deletions
23
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts
This file contains hidden or 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,4 +1,23 @@ | ||
| // @noimplicitany: true | ||
|
|
||
| var x = {}["hello"]; | ||
| var y: string = { '': 'foo' }['']; | ||
| var a = {}["hello"]; | ||
| var b: string = { '': 'foo' }['']; | ||
|
|
||
| // Should give suggestion 'c.get' | ||
| var c = { | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| c['hello']; | ||
|
|
||
| // Should give suggestion 'd.set' | ||
| var d = { | ||
| set: (key: string) => 'foobar' | ||
| }; | ||
| d['hello']; | ||
|
|
||
| // Should give suggestion 'e.get or e.set' | ||
| var e = { | ||
| set: (key: string) => 'foobar', | ||
| get: (key: string) => 'foobar' | ||
| }; | ||
| e['hello']; |
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.
I would try to avoid (and remove) these comments regarding error messages in general. The baselines should communicate that on their own.