microsoft / microsoft/TypeScript
Result of unknown indexing explicitly cast to `any` incorrectly reported as implicit any
Nadie ha tomado este issue todavía.
- 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 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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- 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