Tail recursion optimization limit 999 may be manipulatedΒ #49459
Description
Bug Report
π Search Terms
tail recursion, tail recursion optimization, tail recursion detection, tail recursion limit
π Version & Regression Information
- This is a crash: yes
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about: yes
β― Playground Link
Playground link with relevant code
π» Code
type NumberToTuple1<Num extends number, Tuple extends 0[] = []> = 0 extends 1 ? never :
Tuple['length'] extends Num ? Tuple : NumberToTuple1<Num, [...Tuple, 0]>; // this line
type NumberToTuple2<Num extends number, Tuple extends 0[] = []> =
Tuple['length'] extends Num ? Tuple : NumberToTuple2<Num, [...Tuple, 0]>; // is the same as this line
type BigTuple_Computed = NumberToTuple1<1000>
type BigTuple_Error = NumberToTuple2<1000> // Type instantiation is excessively deep and possibly infinite.(2589)
π Actual behavior
NumberToTuple1
is using the tail recursion optimization so BigTuple_Computed
type is [0, 0, 0, ..., 0]
, just as expected.
NumberToTuple2
is NOT using tail recursion optimization using tail recursion optimization in a different maneer so BigTuple_Error
type is any
.
The difference between them is only in junk piece of code 0 extends 1 ? never :
that is doing nothing.
UPD: NumberToTuple2
is working up to 999, while NumberToTuple1
is working up to 3153
π Expected behavior
According to the article introducing tail call optimization to TypeScript, tail call optimization should be applied to both Recursion limit is expected to remain the sameNumberToTuple1
and NumberToTuple2
the same, because non of them is doing any on-stack transformations with the call results.
Activity