Skip to content

RangeError: Maximum call stack size exceeded (with await nested promises) #35289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kzok opened this issue Nov 22, 2019 · 2 comments
Open

RangeError: Maximum call stack size exceeded (with await nested promises) #35289

kzok opened this issue Nov 22, 2019 · 2 comments
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output
Milestone

Comments

@kzok
Copy link

kzok commented Nov 22, 2019

TypeScript Version: 3.8.0-dev.20191122

Code

  • source code
interface PromiseAction extends Promise<Action> {}
type Action = {type: string} | PromiseAction;
const createFooAction = async (): Promise<Action> => {type: "foo"};
  • tsconfig.json
{
  "compilerOptions": {
    "target": "es2017",
    "allowSyntheticDefaultImports": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
  },
  "include": ["src/**/*"]
}

Expected behavior:

succeed type check or raise some error

Actual behavior:

tsc crashes with RangeError: Maximum call stack size exceeded

$ npm run check

> @ check /Users/okamoto-k/sandbox
> tsc --noEmit

/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:78686
                throw e;
                ^

RangeError: Maximum call stack size exceeded
    at getPromisedTypeOfPromise (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49700:42)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49758:32)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
    at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)

Playground Link:
http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgApQPYFtgGcICCCYwGIyEAHpCACa5qY74A8RJZAfMgN4C+AKDABPAA4p2pcgF5eI8QC5kuMFFABzPsgA+jbHkLEpAbgEIyK5AigQ4kAGIYMkssllxcwkAmQAKAJRK6PqsLiDc0tw8yPIQSgBEME7xyHymQA

Related Issues:

I couldn't find it.

@enisdenjo
Copy link

You made an infinite type loop with your definition:

interface PromiseAction extends Promise<Action> {}
type Action = {type: string} | PromiseAction;

PromiseAction needs the Action, while the Action needs the PromiseAction.

@jcalz
Copy link
Contributor

jcalz commented Nov 22, 2019

Search terms: promise, recursive, crash, async ?

You made an infinite type loop

Yes, but the compiler shouldn't crash as a result. And recursive types are generally acceptable depending on the form such recursion takes. This one looks harmless enough.

I guess the problem is with how async functions are evaluated by the compiler.

type Action = { type: string } | Promise<Action>; // okay TS3.7
function acceptAction(action: Action) { } 
acceptAction({ type: "" }); // okay
acceptAction(new Promise(res => res({ type: "" }))); // okay
acceptAction(Promise.resolve(Promise.resolve(Promise.resolve({ type: "" })))); // okay

function f(): Promise<Action> { return Promise.resolve({ type: "" }) }; // okay
async function g(): Promise<Action> { return Promise.resolve({ type: "" }) }; // kablooey

@kzok kzok changed the title RangeError: Maximum call stack size exceeded (with await nested Promise) RangeError: Maximum call stack size exceeded (with await nested promises) Nov 23, 2019
@andrewbranch andrewbranch added Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output labels Dec 18, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output
Projects
None yet
Development

No branches or pull requests

5 participants