Skip to content

Infinite recursion with generics and recursive Promise type #49646

Closed
@nicolo-ribaudo

Description

@nicolo-ribaudo

Bug Report

🔎 Search Terms

Maximum call stack size exceeded
InternalError: too much recursion
generics type parameter promise recursive crash getAwaitedTypeNoAlias

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions 3.8.3 and 3.9.7
  • It still crashes in 4.8.0-dev.20220622

⏯ Playground Link

Playground link with relevant code (you need to open your browser's console to see the error)

💻 Code

type EnvFunction = <T>() => T;

declare function using<T>(handler: () => T): T;

type SimpleType = string | Promise<SimpleType>;

declare const simple: SimpleType;

const env: EnvFunction = () => using(() => simple);

🙁 Actual behavior

TS crashes due to an infinitely recursive call

RangeError: Maximum call stack size exceeded
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68058:39)
    at mapType (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:58207:85)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68071:60)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68082:35)
    at mapType (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:58207:85)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68071:60)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68082:35)
    at mapType (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:58207:85)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68071:60)
    at getAwaitedTypeNoAlias (/home/nic/Documents/dev/babel/babel/node_modules/typescript/lib/tsc.js:68082:35)

🙂 Expected behavior

Not crash, but maybe report a type error (since env returns SimpleType and not T).

For reference, this comes from some real-world code (that should be valid and not report a type error, but it was a too complex example to use in the issue description): https://github.com/babel/babel/blob/87f26d5a172dae8f6d81a86c9d831e60c99220fe/packages/babel-core/src/config/helpers/config-api.ts#L50-L68

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions