microsoft / microsoft/TypeScript

Trying to flatten an array with unknown depth causes a type error

Aperta
#44,408 3 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

Trying to flatten an array with unknown depth causes a type error.

🔎 Search Terms
  • flat
  • Infinity
🕗 Version & Regression Information
  • Errors in version 4.2.4
  • This changed between versions 3.6.2 and 3.7.5 (but probably just because the type definitions aren't supported prior)

The PR https://github.com/microsoft/TypeScript/pull/32131 provides a definition for FlatArray that has this issue.

⏯ Playground Link

Playground link with relevant code

💻 Code
// first define a nested list with arbitrary depth
type Nested<T> = (T | Nested<T>)[];

const arr: Nested<number> = [1,[2,[3]]];

// locally, this bit fails for me (version 4.2.4)
//
// with an error '"recur"' is referenced directly or indirectly in its own type annotation.
const res: number[] = arr.flat(Infinity);

// you can do a quick workaround as follows:
const res2: number[] = (arr as any[]).flat(Infinity);

// but for some reason on the TypeScript playground this doesn't fail, but you can emulate it
// by using the following definition that is equivalent to the one provided in the standard library:
// https://github.com/microsoft/TypeScript/blob/d8e9f6951919a347bb155b938a5006f9efabb778/lib/lib.es2019.array.d.ts#L21
type FlatArray2<Arr, Depth extends number> = {
    "done": Arr,
    "recur": Arr extends ReadonlyArray<infer InnerArr>
        ? FlatArray2<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]>
        : Arr
}[Depth extends -1 ? "done" : "recur"];

// causes a type error: '"recur"' is referenced directly or indirectly in its own type annotation.
type TestFlat<T> = FlatArray2<Nested<T>, typeof Infinity>
🙁 Actual behavior

The code errors with '"recur"' is referenced directly or indirectly in its own type annotation.

🙂 Expected behavior

The code to pass typechecking.

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.

Direzione di ricerca

Riproduci l’errore con l’esempio fornito di array annidato ed esamina la definizione di FlatArray in lib/lib.es2019.array.d.ts, insieme a PR 32131. Determina in che modo la tipizzazione di flat per una profondità sconosciuta dovrebbe evitare l’errore di tipo ricorsivo, quindi verifica che arr.flat(Infinity) superi il controllo dei tipi senza indebolire il tipo di risultato previsto.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.