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
Better errors for indexing gettable/settable values #26446
Merged
Merged
Changes from all commits
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
45 changes: 42 additions & 3 deletions
45
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.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,8 +1,47 @@ | ||
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(7,1): error TS7052: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'get' ? | ||
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(8,13): error TS7052: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'get' ? | ||
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(13,13): error TS7017: Element implicitly has an 'any' type because type '{ set: (key: string) => string; }' has no index signature. | ||
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(19,1): error TS7052: 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 'set' ? | ||
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(20,1): error TS7052: 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 'set' ? | ||
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(21,1): error TS7052: 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 'set' ? | ||
|
||
|
||
==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (1 errors) ==== | ||
var x = {}["hello"]; | ||
==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (7 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' }['']; | ||
|
||
var c = { | ||
get: (key: string) => 'foobar' | ||
}; | ||
c['hello']; | ||
~~~~~~~~~~ | ||
!!! error TS7052: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'get' ? | ||
const foo = c['hello']; | ||
~~~~~~~~~~ | ||
!!! error TS7052: Element implicitly has an 'any' type because type '{ get: (key: string) => string; }' has no index signature. Did you mean to call 'get' ? | ||
|
||
var d = { | ||
set: (key: string) => 'foobar' | ||
}; | ||
const bar = d['hello']; | ||
~~~~~~~~~~ | ||
!!! error TS7017: Element implicitly has an 'any' type because type '{ set: (key: string) => string; }' has no index signature. | ||
|
||
var e = { | ||
set: (key: string) => 'foobar', | ||
get: (key: string) => 'foobar' | ||
}; | ||
e['hello'] = 'modified'; | ||
~~~~~~~~~~ | ||
!!! error TS7052: 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 'set' ? | ||
e['hello'] += 1; | ||
~~~~~~~~~~ | ||
!!! error TS7052: 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 'set' ? | ||
e['hello'] ++; | ||
~~~~~~~~~~ | ||
!!! error TS7052: 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 'set' ? | ||
|
||
|
45 changes: 41 additions & 4 deletions
45
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.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 |
---|---|---|
@@ -1,7 +1,44 @@ | ||
//// [noImplicitAnyStringIndexerOnObject.ts] | ||
var x = {}["hello"]; | ||
var y: string = { '': 'foo' }['']; | ||
var a = {}["hello"]; | ||
var b: string = { '': 'foo' }['']; | ||
|
||
var c = { | ||
get: (key: string) => 'foobar' | ||
}; | ||
c['hello']; | ||
const foo = c['hello']; | ||
|
||
var d = { | ||
set: (key: string) => 'foobar' | ||
}; | ||
const bar = d['hello']; | ||
|
||
var e = { | ||
set: (key: string) => 'foobar', | ||
get: (key: string) => 'foobar' | ||
}; | ||
e['hello'] = 'modified'; | ||
e['hello'] += 1; | ||
e['hello'] ++; | ||
|
||
|
||
|
||
//// [noImplicitAnyStringIndexerOnObject.js] | ||
var x = {}["hello"]; | ||
var y = { '': 'foo' }['']; | ||
var a = {}["hello"]; | ||
var b = { '': 'foo' }['']; | ||
var c = { | ||
get: function (key) { return 'foobar'; } | ||
}; | ||
c['hello']; | ||
var foo = c['hello']; | ||
var d = { | ||
set: function (key) { return 'foobar'; } | ||
}; | ||
var bar = d['hello']; | ||
var e = { | ||
set: function (key) { return 'foobar'; }, | ||
get: function (key) { return 'foobar'; } | ||
}; | ||
e['hello'] = 'modified'; | ||
e['hello'] += 1; | ||
e['hello']++; |
57 changes: 53 additions & 4 deletions
57
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols
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,9 +1,58 @@ | ||
=== 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)) | ||
|
||
var c = { | ||
>c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 3, 3)) | ||
|
||
get: (key: string) => 'foobar' | ||
>get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 3, 9)) | ||
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 8)) | ||
|
||
}; | ||
c['hello']; | ||
>c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 3, 3)) | ||
|
||
const foo = c['hello']; | ||
>foo : Symbol(foo, Decl(noImplicitAnyStringIndexerOnObject.ts, 7, 5)) | ||
>c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 3, 3)) | ||
|
||
var d = { | ||
>d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 9, 3)) | ||
|
||
set: (key: string) => 'foobar' | ||
>set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 9, 9)) | ||
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 8)) | ||
|
||
}; | ||
const bar = d['hello']; | ||
>bar : Symbol(bar, Decl(noImplicitAnyStringIndexerOnObject.ts, 12, 5)) | ||
>d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 9, 3)) | ||
|
||
var e = { | ||
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 14, 3)) | ||
|
||
set: (key: string) => 'foobar', | ||
>set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 14, 9)) | ||
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 15, 8)) | ||
|
||
get: (key: string) => 'foobar' | ||
>get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 15, 33)) | ||
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 8)) | ||
|
||
}; | ||
e['hello'] = 'modified'; | ||
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 14, 3)) | ||
|
||
e['hello'] += 1; | ||
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 14, 3)) | ||
|
||
e['hello'] ++; | ||
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 14, 3)) | ||
|
||
|
85 changes: 81 additions & 4 deletions
85
tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types
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,15 +1,92 @@ | ||
=== 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" | ||
>'' : "" | ||
|
||
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" | ||
|
||
const foo = c['hello']; | ||
>foo : any | ||
>c['hello'] : any | ||
>c : { get: (key: string) => string; } | ||
>'hello' : "hello" | ||
|
||
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" | ||
|
||
}; | ||
const bar = d['hello']; | ||
>bar : any | ||
>d['hello'] : any | ||
>d : { set: (key: string) => string; } | ||
>'hello' : "hello" | ||
|
||
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'] = 'modified'; | ||
>e['hello'] = 'modified' : "modified" | ||
>e['hello'] : any | ||
>e : { set: (key: string) => string; get: (key: string) => string; } | ||
>'hello' : "hello" | ||
>'modified' : "modified" | ||
|
||
e['hello'] += 1; | ||
>e['hello'] += 1 : any | ||
>e['hello'] : any | ||
>e : { set: (key: string) => string; get: (key: string) => string; } | ||
>'hello' : "hello" | ||
>1 : 1 | ||
|
||
e['hello'] ++; | ||
>e['hello'] ++ : number | ||
>e['hello'] : any | ||
>e : { set: (key: string) => string; get: (key: string) => string; } | ||
>'hello' : "hello" | ||
|
||
|
24 changes: 22 additions & 2 deletions
24
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts
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,4 +1,24 @@ | ||
// @noimplicitany: true | ||
|
||
var x = {}["hello"]; | ||
var y: string = { '': 'foo' }['']; | ||
var a = {}["hello"]; | ||
var b: string = { '': 'foo' }['']; | ||
|
||
var c = { | ||
get: (key: string) => 'foobar' | ||
}; | ||
c['hello']; | ||
const foo = c['hello']; | ||
|
||
var d = { | ||
set: (key: string) => 'foobar' | ||
}; | ||
const bar = d['hello']; | ||
|
||
var e = { | ||
set: (key: string) => 'foobar', | ||
get: (key: string) => 'foobar' | ||
}; | ||
e['hello'] = 'modified'; | ||
e['hello'] += 1; | ||
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.
Remove the stray space at the end.