TypeScript模块出现RangeError: Maximum call stack size exceeded问题求助
问题:运行
npm run serve时出现TypeScript栈溢出错误 最近我在package.json中使用以下依赖执行了npm install:
"dependencies": { "@skynet/api-client": "^2.8.1", "@vue/cli-plugin-vuex": "^4.5.15", "axios": "^1.1.3", "core-js": "^3.18.1", "register-service-worker": "^1.7.1", "single-spa-vue": "^2.1.0", "sweetalert2": "^11.6.7", "vcards-js": "^2.10.0", "vue": "^2.6.11", "vue-i18n": "^8.26.1", "vue-router": "^3.5.4", "vuelidate": "^0.7.7", "vuetify": "^2.6.7", "vuex": "^3.6.2" }, "devDependencies": { "@types/faker": "^5.5.9", "@types/jest": "^27.0.0", "@types/vcards-js": "^2.10.2", "@types/vuelidate": "^0.7.15", "@typescript-eslint/eslint-plugin": "^4.32.0", "@typescript-eslint/parser": "^4.32.0", "@vue/cli-plugin-babel": "~4.5.0", "@vue/cli-plugin-eslint": "~4.5.0", "@vue/cli-plugin-pwa": "~4.5.0", "@vue/cli-plugin-router": "~4.5.0", "@vue/cli-plugin-typescript": "~4.5.0", "@vue/cli-plugin-unit-jest": "^4.5.14", "@vue/cli-service": "^4.5.19", "@vue/eslint-config-typescript": "^7.0.0", "@vue/test-utils": "^1.0.3", "eslint": "^6.7.2", "eslint-plugin-vue": "^6.2.2", "faker": "^5.5.3", "sass": "~1.32.0", "sass-loader": "^8.0.2", "standalone-single-spa-webpack-plugin": "^3.0.0", "ts-jest": "^27.1.5", "typescript": "~4.1.5", "vue-cli-plugin-i18n": "^2.1.4", "vue-cli-plugin-single-spa": "~3.1.2", "vue-cli-plugin-vuetify": "^2.5.1", "vue-template-compiler": "^2.6.11", "vuetify-loader": "^1.7.0" }
当我执行npm run serve运行代码时,出现如下错误:
RangeError: Maximum call stack size exceeded RangeError: Maximum call stack size exceeded at getObjectTypeInstantiation (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57123:44) at instantiateTypeWorker (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57371:28) at instantiateType (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57354:26) at instantiateList (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:56981:34) at instantiateTypes (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:56995:20) at instantiateTypeWorker (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57368:48) at instantiateType (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57354:26) at getMappedType (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57021:79) at C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:57150:82 at Object.map (C:\Projects\Frontend\myApp\node_modules\typescript\lib\typescript.js:643:29)
请问这是否与TypeScript依赖版本有关,还是其他原因?我尝试降低TypeScript版本,但仍然出现该错误,希望得到排查方向。
排查方向
- 检查自定义类型的循环引用:错误栈指向TypeScript的类型实例化和映射类型处理,大概率是项目中存在循环递归的类型定义(比如A类型依赖B,B又反向依赖A,或者映射类型无限递归)。优先排查项目内的
.d.ts文件、业务代码中的interface或type定义。 - 排查第三方依赖的类型冲突:依赖中的
@skynet/api-client、vcards-js等带有类型定义,可能不同依赖的类型定义互相冲突,导致TypeScript解析时陷入循环。可以尝试临时移除某个第三方依赖,观察错误是否消失,以此定位问题包。 - 调整TypeScript编译配置:在
tsconfig.json中尝试关闭部分严格检查选项(比如strict、noImplicitAny),或者将skipLibCheck设为true(跳过第三方库的类型检查),看是否能绕过错误,缩小问题范围。 - 清理依赖缓存:删除
node_modules和package-lock.json,重新执行npm install,避免依赖版本不一致导致的隐藏问题。 - 检查single-spa相关配置:项目使用了
single-spa-vue及对应cli插件,微前端的类型处理可能存在特殊情况,排查single-spa入口文件和类型配置是否存在递归引用。
内容的提问来源于stack exchange,提问作者JDS
相关产品推荐
相关产品推荐

