microsoft / microsoft/TypeScript
Generic constraint is not validated in a recursive type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue in the linked TypeScript Playground using the ComputeRangeLoose and ComputeRangeStrict examples. Trace generic constraint checking for recursive conditional types and verify that both invalid instantiations report the expected constraint diagnostic rather than excessive instantiation depth.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100