From 727a148f51c962a751609a9db97a07039b1a274a Mon Sep 17 00:00:00 2001 From: Collins Abitekaniza Date: Tue, 14 Aug 2018 18:49:40 +0300 Subject: [PATCH] add tests for noImplicitAny indexing on Object --- ...mplicitAnyStringIndexerOnObject.errors.txt | 35 +++++++++- .../noImplicitAnyStringIndexerOnObject.js | 44 +++++++++++-- ...noImplicitAnyStringIndexerOnObject.symbols | 48 ++++++++++++-- .../noImplicitAnyStringIndexerOnObject.types | 65 +++++++++++++++++-- .../noImplicitAnyStringIndexerOnObject.ts | 23 ++++++- 5 files changed, 198 insertions(+), 17 deletions(-) diff --git a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.errors.txt b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.errors.txt index b3f3b4bc6e050..e49b552958e3c 100644 --- a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.errors.txt +++ b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.errors.txt @@ -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' }['']; \ No newline at end of file + 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' ? + \ No newline at end of file diff --git a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.js b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.js index 2ea3291e5db7a..7ccf474550740 100644 --- a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.js +++ b/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']; diff --git a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols index aa3990b0d7c62..280f0784430a9 100644 --- a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols +++ b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.symbols @@ -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)) + diff --git a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types b/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types index a1b78eaca57da..20b517497d245 100644 --- a/tests/baselines/reference/noImplicitAnyStringIndexerOnObject.types +++ b/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" + diff --git a/tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts b/tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts index 9a84cc328b286..d35edbd793488 100644 --- a/tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts +++ b/tests/cases/compiler/noImplicitAnyStringIndexerOnObject.ts @@ -1,4 +1,23 @@ // @noimplicitany: true -var x = {}["hello"]; -var y: string = { '': 'foo' }['']; \ No newline at end of file +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'];