V
V
Vlad2021-08-02 22:54:35
typescript
Vlad, 2021-08-02 22:54:35

How do I fix the "Instance of type is too deep and possibly infinite.ts(2589)" error in my case?

61084b899a4cb001464103.png


I found answers like this:
61084cc0a6611312954766.png


But I don't understand how to use it.
There were such attempts, but they returned an error.


61084c6db45e5291103997.png
61084d1f8d07f505083364.png


I don't need to upgrade to an older version.
I do not know what to do. Help))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-08-03
@TRen208

TypeScript limits the nesting of recursion so that the typechecker works quickly and does not freeze.
You can reduce the nesting if you process several parts at once:

type Split<S extends string, D extends string = ''> = S extends ''
    ? []
    : S extends `${infer H0}${D}${infer H1}${D}${infer H2}${D}${infer H3}${D}${infer H4}${D}${infer H5}${D}${infer Tail}`
        ? [H0, H1, H2, H3, H4, H5, ...Split<Tail, D>]
        : S extends `${infer H0}${D}${infer H1}${D}${infer H2}${D}${infer H3}${D}${infer Tail}`
            ? [H0, H1, H2, H3, ...Split<Tail, D>]
            : S extends `${infer H0}${D}${infer Tail}`
                ? [H0, ...Split<Tail, D>]
                : never;

https://www.typescriptlang.org/play?#code/C4TwDgpg...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question