microsoft / microsoft/TypeScript

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

Aperta
#45,638 2 commenti 1 reazione 1 assegnatario Vedi su GitHub

@elibarzilay ci sta già lavorando.

Dal 2/9/2021.

Needs Investigation Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.