microsoft / microsoft/TypeScript

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

Aperta
#30,323 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci le quattro espressioni con typescript@next usando tsc --strict ./test.ts, quindi traccia il modo in cui vengono prodotti i diagnostici di accesso indicizzato per i casi mostrati. Il lavoro è completato quando x continua a segnalare TS7015, mentre i casi a, y e z convertiti esplicitamente non lo fanno.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.