Repository navigation
When was constraintDepth added? #33541
Description
I was testing some of my code in TS 3.6 and TS 3.7
Code that worked fine in TS 3.5.1 now got the Type_instantiation_is_excessively_deep_and_possibly_infinite error instead.
I decided to do a little investigation and noticed that there was this thing called constraintDepth with a limit of 50. This was what was triggering the error.
I tried to disable the check for this constraintDepth and, predictably, got the error,
return mapper(type);
^
RangeError: Maximum call stack size exceeded
at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42582:24)
at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)
at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)
at instantiateType (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42575:26)
at /home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42274:42
at Object.map (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:507:29)
at getConditionalTypeInstantiation (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42537:40)
at instantiateTypeWorker (/home/anyhowstep/node-projects/tsql/tsql/node_modules/typescript/lib/typescript.js:42620:24)
However, if the code I wrote really was the problem, it shouldn't have run fine in 3.5.1. It should have also exceeded the max call stack size.
I don't have a minimal repro but I noticed something weird.
The following exceeds the constraint depth on 3.6 and 3.7,

However, the following does not,

Trying to nest function calls directly triggers the constraint depth error. But assigning it to a temporary variable is OK.
I'm a bit at a loss here and out of my depth (haha, depth).
- When was
constraintDepthadded? - Why was it added?
- What problem was it trying to solve?
Code that worked fine in TS 3.5.1 now hits this constraintDepth error.
Removing the limit causes the max call stack size to be exceeded.
So, it looks to me like there's a bug with the code surrounding the constraintDepth implementation. Hopefully, the answers to my questions can help me refactor my code to not hit the depth limit. Or maybe it'll expose a bug that needs fixing in the TS code base.
Activity
Repro for microsoft/TypeScript#33541
AnyhowStep commentedon Sep 22, 2019
The file with the problem may be found here,
AnyhowStep/tsql@c413c11#diff-dfb1d0c2f356726e8bd4741b3ba718daR12-R29
cd tsqlc413c1152ae7b65eb2e2a18ee985d6dbb8b31295npm installnpm run test ./test/run-time/input/expr-library/double/acos/double.tsI'd like to again stress that this runs fine on TS 3.5.1 =(
jack-williams commentedon Sep 22, 2019
#26558
44ada08
Added long chain test for coalesce() for microsoft/TypeScript#33541
Added long chain test for coalesce() for microsoft/TypeScript#33541
AnyhowStep commentedon Sep 22, 2019
Also, further proof the functions in question are fine on TS 3.5.1,
AnyhowStep/tsql@f9b73b2#diff-39b2092b0dd378cba12e94f7af61aba7R11-R573
In the above linked file, I've nested
acos()andcoalesce()calls 100+ times (500+ lines).The emit of that giant file is here (only 30+ lines),
AnyhowStep/tsql@f9b73b2#diff-925d183e2fe29d7d151e0add12f6e4eeR2-R36
That file and 500+ others only take 27s to compile with TS 3.5.1,

A screenshot of the file with 100+ nested function calls,

AnyhowStep commentedon Sep 22, 2019
Thanks, Jack Williams (@jack-williams) , I'll start looking from there.
typescript-bot commentedon Sep 28, 2019
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
AnyhowStep commentedon Dec 15, 2019
Instead of trying to figure out which commit broke my
TypeOfCoalesce<>recursive type, I decided to just convert it into a trampoline.The PR on my project,
AnyhowStep/tsql#65
The file that has the new
TypeOfColaesce<>type,https://github.com/AnyhowStep/tsql/pull/65/files#diff-0214b2fefabaf1617e690660683db775R226-R237
If anyone else is running into max depth errors with recursive types, trampolines may be a viable solution.
Here's another example of trampolines being used,
https://github.com/AnyhowStep/tsql/blob/master/src/tuple-util/reverse.ts