microsoft / microsoft/TypeScript
Result of unknown indexing explicitly cast to `any` incorrectly reported as implicit any
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
TypeScript Version: typescript@3.4.0-dev.20190311
Search Terms: "Element implicitly has an 'any' type" " index expression is not of type"
We are on the same page that an unknown/untyped indexing operation results in an any result. However, it seems that silence the error the object being indexed must be cast to any, it is insufficient to cast the result of the indexing itself to any.
Code
declare let foo: HTMLElement;
let a = (<any>foo.attributes)["bar"].value;
let x = foo.attributes["bar"].value;
let y = (<any>foo.attributes["bar"]).value;
let z = (foo.attributes["bar"] as any).value;
compiled with tsc --strict ./test.ts
Expected behavior:
- No error on
let a = ...because type warnings on any operation on an explicitlyanytype (including indexing) are let through. - An error is thrown on
let x = ...because the element type resulting from the lookup is of unknown type. - No error on
let y = ..., not because the type is known but because I'm explicitly casting the<any>result toany. - No error on
let z = ..., not because the type is known but because I'm explicitly casting the<any>result toany.
Actual behavior:
test.ts:3:24 - error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
3 let x = foo.attributes["bar"].value;
~~~~~
test.ts:4:30 - error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
4 let y = (<any>foo.attributes["bar"]).value;
~~~~~
test.ts:5:25 - error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
5 let z = (foo.attributes["bar"] as any).value;
~~~~~
I argue that both (<any>foo)["bar"] and (<any> (foo["bar"])) should not trigger this error.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere die vier Ausdrücke mit typescript@next unter Verwendung von tsc --strict ./test.ts, und verfolge anschließend, wie für die gezeigten Fälle Diagnosen für indizierten Zugriff erzeugt werden. Als abgeschlossen gilt es, wenn x weiterhin TS7015 meldet, während dies bei den explizit gecasteten Fällen a, y und z nicht der Fall ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100