Skip to content

Commit 727a148

Browse files
committed
add tests for noImplicitAny indexing on Object
1 parent 856f754 commit 727a148

5 files changed

+198
-17
lines changed
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts(1,9): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
2+
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' ?
3+
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' ?
4+
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' ?
25

36

4-
==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (1 errors) ====
5-
var x = {}["hello"];
7+
==== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts (4 errors) ====
8+
var a = {}["hello"];
69
~~~~~~~~~~~
710
!!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
8-
var y: string = { '': 'foo' }[''];
11+
var b: string = { '': 'foo' }[''];
12+
13+
// Should give suggestion 'c.get'
14+
var c = {
15+
get: (key: string) => 'foobar'
16+
};
17+
c['hello'];
18+
~~~~~~~~~~
19+
!!! 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' ?
20+
21+
// Should give suggestion 'd.set'
22+
var d = {
23+
set: (key: string) => 'foobar'
24+
};
25+
d['hello'];
26+
~~~~~~~~~~
27+
!!! 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' ?
28+
29+
// Should give suggestion 'e.get or e.set'
30+
var e = {
31+
set: (key: string) => 'foobar',
32+
get: (key: string) => 'foobar'
33+
};
34+
e['hello'];
35+
~~~~~~~~~~
36+
!!! 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' ?
37+
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
//// [noImplicitAnyStringIndexerOnObject.ts]
2-
var x = {}["hello"];
3-
var y: string = { '': 'foo' }[''];
2+
var a = {}["hello"];
3+
var b: string = { '': 'foo' }[''];
4+
5+
// Should give suggestion 'c.get'
6+
var c = {
7+
get: (key: string) => 'foobar'
8+
};
9+
c['hello'];
10+
11+
// Should give suggestion 'd.set'
12+
var d = {
13+
set: (key: string) => 'foobar'
14+
};
15+
d['hello'];
16+
17+
// Should give suggestion 'e.get or e.set'
18+
var e = {
19+
set: (key: string) => 'foobar',
20+
get: (key: string) => 'foobar'
21+
};
22+
e['hello'];
23+
424

525
//// [noImplicitAnyStringIndexerOnObject.js]
6-
var x = {}["hello"];
7-
var y = { '': 'foo' }[''];
26+
var a = {}["hello"];
27+
var b = { '': 'foo' }[''];
28+
// Should give suggestion 'c.get'
29+
var c = {
30+
get: function (key) { return 'foobar'; }
31+
};
32+
c['hello'];
33+
// Should give suggestion 'd.set'
34+
var d = {
35+
set: function (key) { return 'foobar'; }
36+
};
37+
d['hello'];
38+
// Should give suggestion 'e.get or e.set'
39+
var e = {
40+
set: function (key) { return 'foobar'; },
41+
get: function (key) { return 'foobar'; }
42+
};
43+
e['hello'];
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
=== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts ===
2-
var x = {}["hello"];
3-
>x : Symbol(x, Decl(noImplicitAnyStringIndexerOnObject.ts, 0, 3))
2+
var a = {}["hello"];
3+
>a : Symbol(a, Decl(noImplicitAnyStringIndexerOnObject.ts, 0, 3))
44

5-
var y: string = { '': 'foo' }[''];
6-
>y : Symbol(y, Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 3))
5+
var b: string = { '': 'foo' }[''];
6+
>b : Symbol(b, Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 3))
77
>'' : Symbol('', Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 17))
88
>'' : Symbol('', Decl(noImplicitAnyStringIndexerOnObject.ts, 1, 17))
99

10+
// Should give suggestion 'c.get'
11+
var c = {
12+
>c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 3))
13+
14+
get: (key: string) => 'foobar'
15+
>get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 9))
16+
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 5, 8))
17+
18+
};
19+
c['hello'];
20+
>c : Symbol(c, Decl(noImplicitAnyStringIndexerOnObject.ts, 4, 3))
21+
22+
// Should give suggestion 'd.set'
23+
var d = {
24+
>d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 3))
25+
26+
set: (key: string) => 'foobar'
27+
>set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 9))
28+
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 11, 8))
29+
30+
};
31+
d['hello'];
32+
>d : Symbol(d, Decl(noImplicitAnyStringIndexerOnObject.ts, 10, 3))
33+
34+
// Should give suggestion 'e.get or e.set'
35+
var e = {
36+
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 3))
37+
38+
set: (key: string) => 'foobar',
39+
>set : Symbol(set, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 9))
40+
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 17, 8))
41+
42+
get: (key: string) => 'foobar'
43+
>get : Symbol(get, Decl(noImplicitAnyStringIndexerOnObject.ts, 17, 33))
44+
>key : Symbol(key, Decl(noImplicitAnyStringIndexerOnObject.ts, 18, 8))
45+
46+
};
47+
e['hello'];
48+
>e : Symbol(e, Decl(noImplicitAnyStringIndexerOnObject.ts, 16, 3))
49+
Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,72 @@
11
=== tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts ===
2-
var x = {}["hello"];
3-
>x : any
2+
var a = {}["hello"];
3+
>a : any
44
>{}["hello"] : any
55
>{} : {}
66
>"hello" : "hello"
77

8-
var y: string = { '': 'foo' }[''];
9-
>y : string
8+
var b: string = { '': 'foo' }[''];
9+
>b : string
1010
>{ '': 'foo' }[''] : string
1111
>{ '': 'foo' } : { '': string; }
1212
>'' : string
1313
>'foo' : "foo"
1414
>'' : ""
1515

16+
// Should give suggestion 'c.get'
17+
var c = {
18+
>c : { get: (key: string) => string; }
19+
>{ get: (key: string) => 'foobar'} : { get: (key: string) => string; }
20+
21+
get: (key: string) => 'foobar'
22+
>get : (key: string) => string
23+
>(key: string) => 'foobar' : (key: string) => string
24+
>key : string
25+
>'foobar' : "foobar"
26+
27+
};
28+
c['hello'];
29+
>c['hello'] : any
30+
>c : { get: (key: string) => string; }
31+
>'hello' : "hello"
32+
33+
// Should give suggestion 'd.set'
34+
var d = {
35+
>d : { set: (key: string) => string; }
36+
>{ set: (key: string) => 'foobar'} : { set: (key: string) => string; }
37+
38+
set: (key: string) => 'foobar'
39+
>set : (key: string) => string
40+
>(key: string) => 'foobar' : (key: string) => string
41+
>key : string
42+
>'foobar' : "foobar"
43+
44+
};
45+
d['hello'];
46+
>d['hello'] : any
47+
>d : { set: (key: string) => string; }
48+
>'hello' : "hello"
49+
50+
// Should give suggestion 'e.get or e.set'
51+
var e = {
52+
>e : { set: (key: string) => string; get: (key: string) => string; }
53+
>{ set: (key: string) => 'foobar', get: (key: string) => 'foobar'} : { set: (key: string) => string; get: (key: string) => string; }
54+
55+
set: (key: string) => 'foobar',
56+
>set : (key: string) => string
57+
>(key: string) => 'foobar' : (key: string) => string
58+
>key : string
59+
>'foobar' : "foobar"
60+
61+
get: (key: string) => 'foobar'
62+
>get : (key: string) => string
63+
>(key: string) => 'foobar' : (key: string) => string
64+
>key : string
65+
>'foobar' : "foobar"
66+
67+
};
68+
e['hello'];
69+
>e['hello'] : any
70+
>e : { set: (key: string) => string; get: (key: string) => string; }
71+
>'hello' : "hello"
72+
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
// @noimplicitany: true
22

3-
var x = {}["hello"];
4-
var y: string = { '': 'foo' }[''];
3+
var a = {}["hello"];
4+
var b: string = { '': 'foo' }[''];
5+
6+
// Should give suggestion 'c.get'
7+
var c = {
8+
get: (key: string) => 'foobar'
9+
};
10+
c['hello'];
11+
12+
// Should give suggestion 'd.set'
13+
var d = {
14+
set: (key: string) => 'foobar'
15+
};
16+
d['hello'];
17+
18+
// Should give suggestion 'e.get or e.set'
19+
var e = {
20+
set: (key: string) => 'foobar',
21+
get: (key: string) => 'foobar'
22+
};
23+
e['hello'];

0 commit comments

Comments
 (0)