TypeScript编译耗时过长且内存溢出,寻求排查解决方法
TypeScript编译内存溢出&耗时过长问题排查与解决
问题现象
- 执行
yarn tsc --noEmit时触发内存溢出崩溃,报错日志:
<--- Last few GCs ---> [3039:0x158078000] 140607 ms: Scavenge 2031.2 (2084.8) -> 2023.5 (2084.8) MB, 2.0 / 0.0 ms (average mu = 0.633, current mu = 0.461) allocation failure; [3039:0x158078000] 140615 ms: Scavenge 2031.2 (2084.8) -> 2024.8 (2084.8) MB, 2.2 / 0.0 ms (average mu = 0.633, current mu = 0.461) allocation failure; [3039:0x158078000] 140620 ms: Scavenge 2031.3 (2084.8) -> 2024.6 (2101.8) MB, 3.8 / 0.0 ms (average mu = 0.633, current mu = 0.461) allocation failure; <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory MORE TRACES, etc.
- 设置
export NODE_OPTIONS="--max-old-space-size=8192"后,yarn tsc --noEmit --extendedDiagnostics可完成编译,但总耗时长达458.84秒,诊断信息:
Files: 2017 Lines of Library: 9552 Lines of Definitions: 192247 Lines of TypeScript: 78753 Lines of JavaScript: 0 Lines of JSON: 1895 Lines of Other: 0 Identifiers: 332062 Symbols: 1907181 Types: 3051718 Instantiations: 41284341 Memory used: 4369905K Assignability cache size: 304593 Identity cache size: 10450 Subtype cache size: 35521 Strict subtype cache size: 6134 I/O Read time: 0.29s Parse time: 0.59s ResolveModule time: 0.28s ResolveTypeReference time: 0.00s ResolveLibrary time: 0.00s Program time: 1.31s Bind time: 0.30s Check time: 457.23s printTime time: 0.00s Emit time: 0.00s Total time: 458.84s
- 项目实际文件数量远不足2000,但VS Code代码提示、ESLint也出现异常缓慢情况。
- 当前
tsconfig.json配置:
{ "compilerOptions": { "allowJs": false, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "jsx": "react-native", "lib": ["ES2016"], "moduleResolution": "node", "resolveJsonModule": true, "noEmit": true, "strict": true, "skipLibCheck": true, "target": "ES2016", "baseUrl": ".", "paths": { "@app/*": ["./src/*"], "@assets/*": ["./assets/*"] } }, "include": ["./src", "./assets"] }
- 目录说明:
src存储业务代码,assets存储图片资源。
排查与解决步骤
1. 移除无关目录的编译扫描
当前include包含了./assets,但图片资源无需TypeScript解析,反而会让编译器扫描大量非代码文件(甚至误解析文件引发额外类型计算),直接修改tsconfig.json的include字段:
"include": ["./src"]
如果assets下有少量需要解析的JSON配置文件,可单独指定路径,而非包含整个目录:
"include": ["./src", "./assets/config/*.json"]
2. 定位类型实例化爆炸问题
诊断信息中Instantiations达到41284341,这是类型检查耗时的核心原因,通常由复杂泛型、循环类型引用或递归类型导致:
- 执行
tsc --noEmit --generateTrace ./trace生成类型检查追踪文件,用TypeScript官方的Trace Viewer(本地运行即可)分析,定位耗时最长的类型定义或文件。 - 检查代码中的复杂泛型:拆分嵌套过深的泛型、简化频繁使用的联合/交叉类型,避免动态生成大量冗余类型。
- 排查循环依赖:用
tsc --noEmit --listEmittedFiles列出编译文件,找出模块间的循环引用并调整依赖关系。
3. 优化编译配置
- 启用增量编译:生成缓存文件,后续编译仅处理变更文件,大幅降低重复编译耗时:
"compilerOptions": { "incremental": true, "tsBuildInfoFile": "./.tsbuildinfo" } - 针对性关闭严格检查项:如果
strict: true中的某些规则非必须,可单独关闭(如strictNullChecks、strictFunctionTypes),在代码质量与编译速度间做平衡。 - 明确类型定义目录:避免编译器遍历整个
node_modules,指定typeRoots:"compilerOptions": { "typeRoots": ["./node_modules/@types"] }
4. 修复VS Code与ESLint缓慢问题
- VS Code端:
- 禁用非必要插件,只保留官方的「TypeScript and JavaScript Language Features」。
- 在设置中提高TS Server内存上限:
"typescript.tsserver.maxTsServerMemory": 8192 - 执行
Ctrl+Shift+P→TypeScript: Restart TS Server清除缓存。
- ESLint端:
- 检查是否启用了高耗时规则(如
@typescript-eslint/no-unused-vars的all选项),尝试禁用或调整。 - 执行
ESLint: Restart ESLint Server或删除.eslintcache文件清除缓存。
- 检查是否启用了高耗时规则(如
5. 排查依赖包问题
- 检查近期新增的依赖:尤其是类型定义复杂的包(如大型UI库、状态管理库),可能引入大量冗余类型。
- 更新TypeScript到最新稳定版:新版本通常会优化类型检查性能与内存占用。
- 执行
yarn cache clean清除依赖缓存后重新安装,避免依赖损坏引发异常。
内容的提问来源于stack exchange,提问作者Yuniac
相关产品推荐
相关产品推荐

