Empty array filled by push of records is typed number[] inside a function (SC2002)

Chiusa
#1,623 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Una pull request collegata è già stata integrata.

  • #1632 di @ctate — integrata

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Start at the type inference path for empty arrays and push calls inside function bodies, then compare it with the module scope behavior described in the issue. The example reproduces the mismatch with scriptc build evolving.ts; done means the function compiles and prints n0,n1, consistent with TypeScript’s evolving array type.

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

Descrizione

An array declared empty without a type annotation, then filled with push of object literals, compiles at module scope but not inside a function: the array is typed number[].

function build(raw: number[]): { id: number; name: string }[] {
  const nodes = [];
  for (let i = 0; i < raw.length; i++) nodes.push({ id: raw[i]!, name: "n" + i });
  return nodes;
}
console.log(build([4, 5]).map((n) => n.name).join(","));

Expected (Node 24.22.0):

$ node evolving.ts
n0,n1

Actual, with 0.4.0 and with main at 0bb7411:

$ scriptc build evolving.ts
evolving.ts:3:51 - error SC2002: record shapes must match exactly or width-coerce: expected 'number', got '{ id: number; name: string }'
evolving.ts:4:10 - error SC2002: record shapes must match exactly or width-coerce: expected '{ id: number; name: string }[]', got 'number[]'

TypeScript gives nodes the evolving type { id: number; name: string }[] after the loop. The same three lines at module scope compile and print the same as Node.

Lingua principale
TypeScript
Stelle
6.4k
Fork
204
Merge medio
7h 44m
PR unite (30g)
879

Preparare l'ambiente

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.

Altre issue di vercel-labs/scriptc

Tutte le issue di vercel-labs/scriptc

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.