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

iovurdzv  于 10个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(90)

TypeScript版本: 3.8.0-dev.20191122
源代码:

  1. interface PromiseAction extends Promise<Action> {}
  2. type Action = {type: string} | PromiseAction;
  3. const createFooAction = async (): Promise<Action> => {type: "foo"};
  • tsconfig.json
  1. {
  2. "compilerOptions": {
  3. "target": "es2017",
  4. "allowSyntheticDefaultImports": true,
  5. "module": "commonjs",
  6. "moduleResolution": "node",
  7. "noImplicitAny": true,
  8. "noImplicitReturns": true,
  9. "noImplicitThis": true,
  10. "noUnusedLocals": true,
  11. "noUnusedParameters": true,
  12. "strictNullChecks": true,
  13. },
  14. "include": ["src/**/*"]
  15. }

预期行为:

成功进行类型检查或抛出一些错误

实际行为:

tsc崩溃,报错信息为RangeError: Maximum call stack size exceeded

  1. $ npm run check
  2. > @ check /Users/okamoto-k/sandbox
  3. > tsc --noEmit
  4. /Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:78686
  5. throw e;
  6. ^
  7. RangeError: Maximum call stack size exceeded
  8. at getPromisedTypeOfPromise (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49700:42)
  9. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49758:32)
  10. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
  11. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
  12. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
  13. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
  14. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
  15. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)
  16. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49751:46)
  17. at getAwaitedType (/Users/okamoto-k/sandbox/node_modules/typescript/lib/tsc.js:49767:35)

在线示例链接:

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

相关问题:

未找到相关问题。

qlckcl4x

qlckcl4x1#

你用你的定义制造了一个无限类型的循环:

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

PromiseAction 需要 Action ,而 Action 需要 PromiseAction

u4vypkhs

u4vypkhs2#

搜索词:promise,递归,崩溃,async?
你创建了一个无限类型的循环
是的,但编译器不应该因此崩溃。根据这种递归的形式,递归类型通常是可以接受的。这个看起来足够无害。
我想问题出在编译器如何评估async函数上。

  1. type Action = { type: string } | Promise<Action>; // okay TS3.7
  2. function acceptAction(action: Action) { }
  3. acceptAction({ type: "" }); // okay
  4. acceptAction(new Promise(res => res({ type: "" }))); // okay
  5. acceptAction(Promise.resolve(Promise.resolve(Promise.resolve({ type: "" })))); // okay
  6. function f(): Promise<Action> { return Promise.resolve({ type: "" }) }; // okay
  7. async function g(): Promise<Action> { return Promise.resolve({ type: "" }) }; // kablooey

相关问题