TypeScript: 5.1 regression from 5.0: JavaScript heap out of memory (Type instantiation is excessively deep and possibly infinit)

Bug Report

5.0.4 used to work fine after upgrading to the current latest version 5.1.3. Very slow builds and eventually crashes. My root-cause seems to be a circular issue of some sort with tsc.

Seems like tsc can’t handle interface definition files anymore, in this case from lodash?

node_modules/@types/lodash/common/common.d.ts:194:15 - error TS2589: Type instantiation is excessively deep and possibly infinite.

194     interface Object<T> extends LoDashImplicitWrapper<T> {
                  ~~~~~~

node_modules/@types/lodash/common/common.d.ts:206:15 - error TS2430: Interface 'ObjectChain<T>' incorrectly extends interface 'LoDashExplicitWrapper<T>'.
  The types returned by 'entries().pop()' are incompatible between these types.
    Type 'CollectionChain<string | T[keyof T]>' is missing the following properties from type 'ObjectChain<[string, any]>': assign, assignIn, assignInWith, assignWith, and 11 more.

206     interface ObjectChain<T> extends LoDashExplicitWrapper<T> {
                  ~~~~~~~~~~~

My workaround was adding "skipLibCheck": true to the tsconfig.json did mitigate the issue for now.

node_modules/@types/lodash/common/common.d.ts:194:15 - error TS2589: Type instantiation is excessively deep and possibly infinite.

194     interface Object<T> extends LoDashImplicitWrapper<T> {
                  ~~~~~~

node_modules/@types/lodash/common/common.d.ts:206:15 - error TS2430: Interface 'ObjectChain<T>' incorrectly extends interface 'LoDashExplicitWrapper<T>'.
  The types returned by 'entries().pop()' are incompatible between these types.
    Type 'CollectionChain<string | T[keyof T]>' is missing the following properties from type 'ObjectChain<[string, any]>': assign, assignIn, assignInWith, assignWith, and 11 more.

206     interface ObjectChain<T> extends LoDashExplicitWrapper<T> {
                  ~~~~~~~~~~~

Workaround for me was to add: "skipLibCheck": true to the tsconfig.json did mitigate the issue for now.

🔎 Search Terms

  • Crash
  • OOME
  • 5.1
  • Heap

🕗 Version & Regression Information

I also tried the latest next tag, with the same issues.

  • This is a crash
  • This changed between versions 5.0.x and 5.1.x

⏯ Playground Link

The “Type is excessively deep” is being triggered in a project that includes this the @types/lodash, so in your package.json be sure you have the dependency:

"dependencies": {
     "lodash": "^4.17.21"
},
"devDependencies": {
    "@types/lodash": "^4.14.195"   
}

Be sure that you did NOT set skipLibCheck to true. So keep it to false, otherwise the d.ts files will be ignored.

Execute via: NODE_OPTIONS=--max-old-space-size=8192 tsc --pretty --diagnostics --extendedDiagnostics --incremental false

💻 Code

See below, it doesn’t involve my code.

🙁 Actual behavior

JavaScript heap out of memory, because of circular/ too deep dependency?

node_modules/@types/lodash/common/common.d.ts:194:15 - error TS2589: Type instantiation is excessively deep and possibly infinite.

194     interface Object<T> extends LoDashImplicitWrapper<T> {
                  ~~~~~~

node_modules/@types/lodash/common/common.d.ts:206:15 - error TS2430: Interface 'ObjectChain<T>' incorrectly extends interface 'LoDashExplicitWrapper<T>'.
  The types returned by 'entries().pop()' are incompatible between these types.
    Type 'CollectionChain<string | T[keyof T]>' is missing the following properties from type 'ObjectChain<[string, any]>': assign, assignIn, assignInWith, assignWith, and 11 more.

206     interface ObjectChain<T> extends LoDashExplicitWrapper<T> {
                  ~~~~~~~~~~~
Found 2 errors in 1 file.

Errors  Files
     2  node_modules/@types/lodash/common/common.d.ts:194
Files:                          253
Lines of Library:              9875
Lines of Definitions:         73521
Lines of TypeScript:           3999
Lines of JavaScript:              0
Lines of JSON:                    0
Lines of Other:                   0
Identifiers:                 117524
Symbols:                    4856905
Types:                      1093043
Instantiations:            11883895
Memory used:               2379915K
Assignability cache size:    230447
Identity cache size:            772
Subtype cache size:             398
Strict subtype cache size:       10
I/O Read time:                0.15s
Parse time:                   1.17s
ResolveModule time:           0.21s
ResolveTypeReference time:    0.03s
ResolveLibrary time:          0.02s
Program time:                 1.70s
Bind time:                    0.47s
Check time:                 239.51s
transformTime time:           0.05s
Source Map time:              0.02s
commentTime time:             0.04s
I/O Write time:               0.02s
printTime time:               0.37s
Emit time:                    0.37s
Total time:                 242.05s

🙂 Expected behavior

No crashes or errors during tsc!

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 40
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Any update on this? Got the same issue when upgrading to typescript 5.1.3 from 4.9.4 and we have @types/lodash : 4.14.195 as devdependencies.

Any chance to fix this? 3 weeks after issue was reported.

My workaround for now is to disable interfaces (.d.ts files), using the following code snippet in my local tsconfig.json file:

  "skipLibCheck": true,

More info: https://www.typescriptlang.org/tsconfig#skipLibCheck

Can people following the thread verify that https://github.com/microsoft/TypeScript/pull/54781 fixes the issue? You can do so by using a specific build of TypeScript:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/155637/artifacts?artifactName=tgz&fileId=04DE374095785839F62F13D7F462400B471B8FC647A291E41CD31D23BFD6186002&fileName=/typescript-5.2.0-insiders.20230626.tgz"
    }
}

(see https://github.com/microsoft/TypeScript/pull/54781#issuecomment-1607903315)

My workaround for now is to disable interfaces (.d.ts files), using the following code snippet in my local tsconfig.json file:

  "skipLibCheck": true,

More info: https://www.typescriptlang.org/tsconfig#skipLibCheck

Important to mention that it is a just temporary workaround as it would skip all libraries check.

Same problem with lodash here but with skipLibCheck to true. Any news about this resolution ?

It works fine with typescript 5.0.4

This is my error :

 ../../node_modules/@types/lodash/common/common.d.ts(194,15): error TS2589: Type instantiation is excessively deep and possibly infinite.
       ../../node_modules/@types/lodash/common/common.d.ts(206,15): error TS2430: Interface 'ObjectChain<T>' incorrectly extends interface 'LoDashExplicitWrapper<T>'.
         The types returned by 'entries().pop()' are incompatible between these types.
           Type 'CollectionChain<string | T[keyof T]>' is missing the following properties from type 'ObjectChain<[string, any]>': assign, assignIn, assignInWith, assignWith, and 11 more.

I can repo this with a simple single-file project that just imports @types/lodash, but only when strict mode is disabled:

// compile without --strict
import "lodash";

No problems when compiling with --strict enabled. The runaway type check happens when computing variance information for type ObjectChain<T>. A quick workaround is to change one of the declarations of ObjectChain<T> to ObjectChain<in out T>. This asserts that the type is invariant which skips the variance computation.