Skip to content

Commit

Permalink
add tests for noImplicitAny indexing on Object
Browse files Browse the repository at this point in the history
  • Loading branch information
collin5 committed Aug 14, 2018
1 parent 24c5912 commit 5097ed1
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 17 deletions.
@@ -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 TS7042: 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 TS7042: 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 TS7042: 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 TS7042: 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 TS7042: 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 TS7042: 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
@@ -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'];
@@ -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
@@ -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
@@ -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'];

0 comments on commit 5097ed1

Please sign in to comment.