Typescript types - Now resolve "Type instantiation is excessively deep and possibly infinite.(2589)"

125 views Asked by At

I'm playing with Typescript Types, and I'm hit an error that I can't resolve and also not fully understand.

Error

The error is the one mentioned on the title: Type instantiation is excessively deep and possibly infinite.

I've read some material:

  1. recursive conditional type
  2. tail recursion elimination on conditional types

but I'm not able to resolve it for my specific case.

Code

Here my (simplified) code, that you can also view on the playground

type OneLemonPlusSomeApples<INPUT extends number> = [ '', ...CreateElements<INPUT, ''>] //<-- Type instantiation is excessively deep and possibly infinite.(2589)


type CreateElements<I extends number, E extends string, A extends string[]=[]> = 
I extends A["length"] ? A : CreateElements<I, E, [...A, E]>;


type aLemonAndThreeApples = OneLemonPlusSomeApples<3>;
//   ^?

type aLemonAndTwoApples = OneLemonPlusSomeApples<2>;
//   ^?

Why I'm getting the error when I'm using the spread operator (...)?

Note

As you might notice I'm using the tuple A as an accumulator and a way of keeping track of the iteration.

Thanks for the help.

0

There are 0 answers