microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.7k stars 12.45k forks source link

Recursive types error: Type instantiation is excessively deep #45672

Open vlazh opened 3 years ago

vlazh commented 3 years ago

Bug Report

I had got error Type instantiation is excessively deep and possibly infinite. in my JSON types - value to JSON transformation. It's some new limitation introduced from v4.3.2? Works fine with v4.2.4.

🕗 Version & Regression Information

⏯ Playground Link

I couldn't get to make a short and simple example because when I removing any recursive type reference then error will disappeared.

Playground link with relevant code

andrewbranch commented 3 years ago

Re-opened from #44281, possibly related: #45632, #45576

andrewbranch commented 3 years ago

@weswigham didn't we start doing more to relate conditional types around that time frame? I seem to recall some increase in these kinds of errors was expected. But this example takes a super long time to even finish checking, and it’s nearly instant in 4.2.

weswigham commented 3 years ago

We did definitely add rules for relating conditional type constraints around then, I think.

andrewbranch commented 3 years ago

https://github.com/microsoft/TypeScript/pull/37208?

andrewbranch commented 3 years ago

Indeed, reverting that “fixes” this issue.

jakearchibald commented 3 years ago

I think this issue is impacting a couple of my libraries, as the IDBValidKey type references itself.

Here's the pattern that's causing a problem. It isn't a problem in 4.3.5.

andrewbranch commented 3 years ago

Spoke with @weswigham about this and he mentioned we might be able to apply a targeted fix by swapping the order of checks for conditional types.

vlazh commented 2 years ago

It looks like the issue was fixed by the version 4.6.2. Isn't it?

jakearchibald commented 2 years ago

This example still fails in 4.6.2. Not sure if that's intended.

vlazh commented 2 years ago

This example still fails in 4.6.2. Not sure if that's intended.

I found two deep recursions: IDBValidKey and IsExact -> DeepMakeRequiredForIsExact. If splits IDBValidKey into two types the error goes away. But they not the same. Not allowed IDBValidKey[][][]... Example.

jakearchibald commented 2 years ago

But this seemed to work fine in 4.3.5

hasgar-aot commented 2 years ago

any fix? it's kind of annoying v4.0.3 fails in 4.3.5 too.