A series of nested loops, undefined and `in` checks results in incorrect implicit any

Abierto
#45,638 2 comentarios 1 reacción 1 asignado Ver en GitHub

@elibarzilay ya está trabajando en esto.

Desde el 2/9/2021.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

Needs Investigation Rescheduled

Bug Report

A series of nested loops, undefined and in checks results in incorrect implicit any.

I don't know what is really causing this. It seems this might be a weird edge case where type inference fails?

🔎 Search Terms

noImplicitAny, inference

🕗 Version & Regression Information

This changed between versions 4.3.5 and 4.4.2

⏯ Playground Link

Playground link with relevant code

💻 Code
interface MyObj { id?: string; }
declare var objects: MyObj[]|undefined;

if (objects) {
  for (const _ of []) {
    const map: {[key:string]: string|undefined} = {};

    for (const obj of objects) {
      //       ~~~ 'obj' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
      const id = obj.id!;
      //    ~~ 'id' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
      if (id in map) {
        console.log(id);
      }
    }
  }
}
🙁 Actual behavior

In version 4.4.2 the above code fails to compile with the 2 errors.

Any of the following changes cause it compile without errors:

  • Remove |undefined from the objects type.
  • Remove the outer for (const _ of []) loop
  • Remove if (id in map)
  • Add a type annotation to the id variable: const id: string = obj.id!
🙂 Expected behavior

The code should compile without errors as it did in version 4.3.5.

Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

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.

Más de microsoft/TypeScript

Todos los issues de microsoft/TypeScript

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.