microsoft / microsoft/TypeScript

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

Abierto
#30,323 0 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

In Discussion Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce las cuatro expresiones con typescript@next usando tsc --strict ./test.ts y, a continuación, rastrea cómo se generan los diagnósticos de acceso indexado para los casos mostrados. Se considera terminado cuando x sigue notificando TS7015, mientras que los casos a, y y z convertidos explícitamente no lo hacen.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.