npm build失败:@types/lodash类型定义报错,求解决方案
Angular构建时@types/lodash类型定义报错TS1005和TS2536的解决办法
环境信息
- Node: 16.20.2
- Angular CLI: 11.2.5
- Typescript: 4.1.5
- @types/lodash: 4.14.177
报错详情
Error: node_modules/@types/lodash/common/object.d.ts:1026:46 error TS1005: '?' expected. 1026 : K extends $(infer N extends number)? T[N] Error: node_modules/@types/lodash/common/object.d.ts:1026:51 error TS2536: Type 'N' cannot be used to index type 'T'. 1026 K extends $(infer N extends number)? T[N] Error: node_modules/@types/lodash/common/object.d.ts:1031:46 error TS1005: '?' expected. 1031 K extends $(infer N extends number)?[N] Error: node_modules/@types/lodash/common/object.d.ts:1031:51 error TS2536: Type 'N' cannot be used to index type 'T'. 1031 : extends $(infer i extends number) [N] Error: node_modules/@types/lodash/common/object.d.ts:1041:46 error TS1005: '?' expected. 1041 extends $infer i extends number)
问题原因
TypeScript 4.1.5不支持@types/lodash 4.14.177中引入的部分高级类型语法(带$的infer扩展写法),这些特性需要更高版本的TypeScript才能兼容。但Angular 11.2.5对TypeScript版本有严格限制(最高支持4.1.x),无法直接升级TypeScript。
解决办法
直接降级@types/lodash到兼容TypeScript 4.1的版本,推荐使用4.14.165版本:
npm install @types/lodash@4.14.165 --save-dev
安装完成后重新执行npm run build即可解决报错。
内容的提问来源于stack exchange,提问作者atal anand
相关产品推荐
相关产品推荐

