How to Fix TypeScript Error TS2589 “Type instantiation is excessively deep”
error TS2589: Type instantiation is excessively deep and possibly infinite. appears when the computation of a type that calls itself (a recursive type) exceeds the limits TypeScript sets for nesting depth and repetition count. It goes away when you rewrite the type in "tail-recursive" form by adding an accumulator argument that carries the intermediate result, or when you reject inputs that would never terminate before the recursion starts.
This error is common when you write a recursive type yourself, such as a type that processes a string literal type one character at a time or a type that builds a tuple of a given length. Below, I reproduce the error, fix it, and confirm with tsc that it goes away.
The test environment was Ubuntu 24.04, Node.js 22.23.5, npm 10.9.9, and TypeScript 7.0.2. I also ran the main examples with TypeScript 5.9.3. I did not test TS2589 caused by library types (such as those in validation or form libraries), and I did not test how editors such as VS Code display it.
- 1. Causing TS2589 with a type that reverses a string
- 2. The type that errors has stopped computing partway
- 3. The non-tail-recursive version hits TS2589 at 49 characters
- 4. Adding an accumulator to make it tail-recursive
- 5. Even tail recursion stops at 1000 iterations
- 6. Recursion that never reaches its end condition: reject the input
- 7. TypeScript 5.9 gave the same results
Causing TS2589 with a type that reverses a string
In an empty folder, I ran npm init -y and then npm install -D typescript@7.0.2 to install TypeScript 7.0.2. The tsconfig.json was as follows.
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"target": "es2022",
"strict": true,
"noEmit": true
},
"include": ["src"]
}
In src/reverse.ts, I wrote a Reverse type that reverses a string literal type. It splits the string into the first character Head and the rest Rest, then appends Head after the reversed Rest. I passed a short "abc" and a 52-character string made by writing the alphabet twice.
type Reverse<S extends string> =
S extends `${infer Head}${infer Rest}` ? `${Reverse<Rest>}${Head}` : "";
type Short = Reverse<"abc">;
const short: Short = "cba";
type Long = Reverse<"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz">;
Running npx tsc produced one error on line 7, and the exit code was 1.
$ npx tsc
src/reverse.ts(7,13): error TS2589: Type instantiation is excessively deep and possibly infinite.
The message means that the type instantiation (the process of applying type arguments to build a type) is too deep and may continue infinitely. With --pretty, a squiggly underline appears under Reverse<...> (colors omitted here).
src/reverse.ts:7:13 - error TS2589: Type instantiation is excessively deep and possibly infinite.
7 type Long = Reverse<"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz">;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in src/reverse.ts:7
Short, which is 3 characters, has no error. The type definition is not wrong. The error appears only when the input is long enough for the recursion to get deep.
The type that errors has stopped computing partway
To see what type Long became, I removed the two lines of Short and the blank line below them, and added a line that assigns a different string. Then I ran npx tsc.
type Reverse<S extends string> =
S extends `${infer Head}${infer Rest}` ? `${Reverse<Rest>}${Head}` : "";
type Long = Reverse<"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz">;
const wrong: Long = "zzz";
$ npx tsc
src/reverse.ts(4,13): error TS2589: Type instantiation is excessively deep and possibly infinite.
src/reverse.ts(5,7): error TS2322: Type '"zzz"' is not assignable to type '`${any}${any}wvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba`'.
The type shown in the second message has its last 49 characters (the reversed first 49 characters of the input) computed correctly, but the part before them is ${any}${any}. The computation stopped when it hit the limit, and the remaining positions were filled with any. So the type where TS2589 appears is not a correctly computed type.
The non-tail-recursive version hits TS2589 at 49 characters
I tried the same Reverse with a string of n a characters. The file contained the two lines of the Reverse definition, a blank line, and type Test = Reverse<"aaa…a">; on line 4. With 47 and 48 characters the exit code was 0, and with 49 and 50 characters TS2589 appeared.
$ npx tsc
src/len.ts(4,13): error TS2589: Type instantiation is excessively deep and possibly infinite.
In the TypeScript 7 type checker source (checker.go on the main branch of microsoft/typescript-go, as of November 2026), this error is reported when type instantiation nests 100 levels deep, or when a single statement or expression reaches 5 million operations. Reverse calls itself once per character and uses that result inside `${Reverse<Rest>}${Head}`, so the nesting grows with every character. For this type, inputs up to 48 characters stayed within the limits.
Adding an accumulator to make it tail-recursive
The TypeScript 4.5 release notes explain that when a branch of a conditional type returns another conditional type directly (a self-call in that position is tail recursion), the intermediate instantiations are not created and the limit is much higher. The same page recommends adding a type parameter that carries the intermediate result (an accumulator) to make a type tail-recursive when the type also does work with its results.
I added an Acc parameter to Reverse that holds the partly reversed string. Each step prepends the first character to Acc and passes the result on. At the end, the function returns Acc.
type Reverse<S extends string, Acc extends string = ""> =
S extends `${infer Head}${infer Rest}` ? Reverse<Rest, `${Head}${Acc}`> : Acc;
type Short = Reverse<"abc">;
const short: Short = "cba";
type Long = Reverse<"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz">;
const long: Long = "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba";
Acc has a default value of = "", so callers can still write Reverse<"abc"> as before. npx tsc printed nothing and exited with code 0. Assigning the reversed 52-character string to Long also produced no error.
To confirm the resulting type is correct, I assigned a different string. I ran npx tsc on the following file, and the message showed the full reversed 52-character string.
type Reverse<S extends string, Acc extends string = ""> =
S extends `${infer Head}${infer Rest}` ? Reverse<Rest, `${Head}${Acc}`> : Acc;
type Long = Reverse<"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz">;
const wrong: Long = "zzz";
$ npx tsc
src/reverse.ts(5,7): error TS2322: Type '"zzz"' is not assignable to type '"zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba"'.
Unlike the version before the rewrite, the type contains no ${any}.
Even tail recursion stops at 1000 iterations
Tail recursion does not remove the limit entirely. With the rewritten Reverse, strings of n a characters gave exit code 0 at 998 and 999 characters, and TS2589 at 1000 and 1001 characters.
$ npx tsc
src/len.ts(4,13): error TS2589: Type instantiation is excessively deep and possibly infinite.
The TypeScript pull request #45711, which added tail-recursion handling, says that such a type is evaluated up to 1000 times and then reported as an error, treating it as non-terminating. The checker.go source I cited above also has logic that counts tail-recursive steps and reports this error at 1000. If you need to process an input of 1000 characters or more in a type, you should reconsider the approach you take in the type.
Recursion that never reaches its end condition: reject the input
As the message "possibly infinite" suggests, the same TS2589 appears when the recursion never reaches its end condition. The following Tuple type adds one element at a time and stops when the length reaches N.
type Tuple<T, N extends number, Acc extends T[] = []> =
Acc["length"] extends N ? Acc : Tuple<T, N, [...Acc, T]>;
const pair: Tuple<string, 2> = ["a", "b"];
const half: Tuple<string, 2.5> = [];
Tuple<string, 2> becomes [string, string]. An array length can never be 2.5, so the recursion for Tuple<string, 2.5> never ends. With npx tsc, TS2589 appeared on line 5 for Tuple<string, 2.5>.
$ npx tsc
src/tuple.ts(5,13): error TS2589: Type instantiation is excessively deep and possibly infinite.
In this case, instead of using an accumulator, I rejected the inputs that never terminate before the recursion begins. If `${N}` (the string form of N) starts with - or contains ., the number is negative or fractional, so I made the type return never.
type Tuple<T, N extends number, Acc extends T[] = []> =
`${N}` extends `-${string}` | `${string}.${string}` ? never
: Acc["length"] extends N ? Acc : Tuple<T, N, [...Acc, T]>;
const pair: Tuple<string, 2> = ["a", "b"];
npx tsc exited with code 0. When the rejected inputs are used, you now get an error that shows what is wrong, instead of TS2589. I added lines using Tuple<string, 2.5>, Tuple<string, -1>, and Tuple<string, 3> to the file above and ran it.
const half: Tuple<string, 2.5> = [];
const minus: Tuple<string, -1> = [];
const short: Tuple<string, 3> = ["a", "b"];
$ npx tsc
src/tuple.ts(6,7): error TS2322: Type 'never[]' is not assignable to type 'never'.
src/tuple.ts(7,7): error TS2322: Type 'never[]' is not assignable to type 'never'.
src/tuple.ts(8,7): error TS2322: Type '[string, string]' is not assignable to type '[string, string, string]'.
Source has 2 element(s) but target requires 3.
For 2.5 and -1, the type is never, so even [] cannot be assigned and TS2322 is reported. Tuple<string, 3> is computed correctly as [string, string, string], and TS2322 shows that the array is missing an element.
TypeScript 5.9 gave the same results
I also ran the TS2589 example and the fixed examples with TypeScript 5.9.3. The TS2589 message, the error positions, and the display of the type filled with ${any} matched TypeScript 7.0.2 exactly. The non-tail-recursive Reverse passed up to 48 characters, and the tail-recursive Reverse passed up to 999 characters. TS2589 appeared at the next length in both cases, as it did with TypeScript 7.0.2. The Reverse with an accumulator and the Tuple that rejects inputs both exited with code 0.
The difference was the exit code of tsc when there were errors. It was 1 with TypeScript 7.0.2 and 2 with TypeScript 5.9.3. If you have a script that checks for exit code 2, that check will change its result when you move to TypeScript 7, because the exit code is now 1.





