microsoft / microsoft/TypeScript

Type instantiation is excessively deep with tuple spread

Aperta
#54,910 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: Conditional Types Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms
  • 2889 spread
🕗 Version & Regression Information
  • This is the behavior in every version I tried (4.1.5 to 5.2.0 nightly), and I reviewed the FAQ for entries about: recursion
⏯ Playground Link

Playground link with relevant code

💻 Code
type NTuple<T, N extends number, Acc extends T[] = []> =
  N extends Acc['length']
  ? Acc
  : NTuple<T, N, [...Acc, T]>

type Pair = NTuple<number, 2>;
//   ^? type Pair = [number, number]

type SpreadN<A extends number> = [...NTuple<any, A>]
//                               ~~~~~~~~~~~~~~~~~~~
// Type instantiation is excessively deep and possibly infinite. (2589)
🙁 Actual behavior

I'm getting the "Type instantiation is excessively deep and possibly infinite. (2589)" error on what should be the identity.

🙂 Expected behavior

I'd expect this to just work. SpreadN should produce the same results as NTuple. It's surprising to me that this error is produced on the definition of SpreadN, rather than when it's instantiated with an overly large value of N.

I think this may have something to do with distribution over unions because when I reverse the conditional, the code works fine:

type NTuple2<T, N extends number, Acc extends T[] = []> =
  Acc['length'] extends N  // note that the check is reversed
  ? Acc
  : NTuple2<T, N, [...Acc, T]>

type SpreadN2<A extends number> = [...NTuple<any, A>]  // ok
type Pair2 = SpreadN2<2>;
//   ^? type Pair2 = [any, any]

It's not clear to me why reversing that conditional would matter, but it does have the effect of preventing distribution over unions, which is not desirable:

type T1 = NTuple<string, 2 | 3>;
//   ^? type T1 = [string, string] | [string, string, string]
type T2 = NTuple2<string, 2 | 3>;
//   ^? type T2 = [string, string]

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

Inizia riproducendo l'esempio di spread di tuple nel TypeScript Playground collegato e confronta NTuple con NTuple2, incluso il loro comportamento sulle unioni. Il lavoro è completato quando SpreadN non segnala più l'errore 2589 nella sua definizione, mentre NTuple mantiene i risultati attesi delle unioni distribuite.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.