Angular构建失败:@smithy/types触发TS1337索引签名类型错误
Angular构建报错:TS1337 索引签名参数类型不能是联合类型
报错详情
ERROR in node_modules/@smithy/types/dist-types/ts3.4/extensions/checksum.d.ts:23:6 - error TS1337: An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
对应报错代码行:
[other: string | number]: any;
环境信息
- Angular 10.2.4
- Node 14.21.3
- TypeScript 3.9.4
- 已尝试所有版本的
@smithy/types
此前曾在TS 4.0版本遇到相同问题,通过降级到TS 3.9.1并安装对应依赖解决,但当前调整版本后问题仍未解决。
解决方法
1. 跳过第三方库类型检查(快速临时解决)
在项目根目录的tsconfig.json中启用skipLibCheck配置,直接绕过对第三方库的类型校验:
{ "compilerOptions": { // 保留原有其他配置 "skipLibCheck": true } }
此方法不会修复库本身的类型问题,但能快速让构建流程正常完成,适合紧急上线场景。
2. 手动修补类型文件并持久化补丁
找到node_modules/@smithy/types/dist-types/ts3.4/extensions/checksum.d.ts文件,将报错的索引签名替换为映射类型:
原代码:
[other: string | number]: any;
修改为:
[key in string | number]: any;
为避免npm install后修改被覆盖,可使用patch-package工具持久化补丁:
- 安装工具:
npm install patch-package --save-dev - 生成补丁:修改文件后运行
npx patch-package @smithy/types - 自动应用补丁:在
package.json的scripts中添加"postinstall": "patch-package",确保每次安装依赖后自动执行补丁。
3. 锁定兼容的依赖版本组合
Angular 10.2.4官方推荐的TypeScript版本范围是~3.9.0,尝试锁定TS 3.9.1并搭配兼容的@smithy/types版本:
- 卸载当前依赖:
npm uninstall typescript @smithy/types - 安装指定版本:
npm install typescript@3.9.1 @smithy/types@^1.0.0 --save-exact
注意:需确认低版本@smithy/types与你项目中其他AWS相关依赖的兼容性。
内容的提问来源于stack exchange,提问作者siddsharma25
相关产品推荐
相关产品推荐

