microsoft / microsoft/TypeScript
Generic constraint is not validated in a recursive type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🕗 Version & Regression Information
- This is the behavior in every version I tried, starting from 4.1 where recursive conditional types were added
⏯ Playground Link
Playground link with relevant code
💻 Code
export type ComputeRangeLoose<
N extends number,
Result extends Array<number> = [],
> = Result['length'] extends N ? Result : ComputeRangeLoose<N, [...Result, Result['length']]>
type A = ComputeRangeLoose<{ foo: true }>
export type ComputeRangeStrict<
N extends number,
Result extends Array<number> = [],
> = N extends number
? Result['length'] extends N ? Result : ComputeRangeStrict<N, [...Result, Result['length']]>
: never
type B = ComputeRangeStrict<{ foo: true }>
🙁 Actual behavior
ComputeRangeLoose<{ foo: true }> allows to pass a type that is not related to number, throwing Type instantiation is excessively deep and possibly infinite.
ComputeRangeStrict<{ foo: true }> throws an expected Type '{ foo: true; }' does not satisfy the constraint 'number'
🙂 Expected behavior
Both ComputeRangeLoose and ComputeRangeStrict throw Type '{ foo: true; }' does not satisfy the constraint 'number'
As the issue is reproducible since 4.1, I'm pretty sure it was already mentioned somewhere however I failed to find a suitable issue
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo il problema nel TypeScript Playground collegato usando gli esempi ComputeRangeLoose e ComputeRangeStrict. Traccia il controllo dei vincoli generici per i tipi condizionali ricorsivi e verifica che entrambe le istanziazioni non valide riportino la diagnostica dei vincoli prevista invece di una profondità di istanziazione eccessiva.
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