microsoft / microsoft/TypeScript

Result of unknown indexing explicitly cast to `any` incorrectly reported as implicit any

Offen
#30,323 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
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 explicitly any type (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 to any.
  • No error on let z = ..., not because the type is known but because I'm explicitly casting the <any> result to any.

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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.