Skip to content

Tail recursion optimization limit 999 may be manipulatedΒ #49459

Open
@Alexsey

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 NumberToTuple1 and NumberToTuple2 the same, because non of them is doing any on-stack transformations with the call results. Recursion limit is expected to remain the same

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions