Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests for noImplicitAny indexing on Object
  • Loading branch information
collin5 committed Apr 30, 2019
commit b66fea6e4c9fdf608311305daff0a531fc5a7d05
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 tests/baselines/reference/noImplicitAnyStringIndexerOnObject.js
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'
Copy link
Copy Markdown
Member

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.

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'];
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 tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types
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 tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts
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'];