Angular构建报错TS2300:@firebase/auth-compat中'type'标识符重复
执行ng build构建Angular 12项目时,出现以下类型错误:
Error: node_modules/@firebase/auth-compat/dist/auth-compat/index.d.ts:53:10 - error TS2300: Duplicate identifier 'type'.
涉及带type前缀的导入语句(如import { type User, type Unsubscribe,... } from "@firebase/auth";),且database-compat、storage-compat等Firebase compat包均可能触发该错误,即使降级firebase和@angular/fire版本也无法解决。
解决方案
1. 强制锁定Firebase compat包版本
问题根源是间接依赖的Firebase compat包版本未与你指定的firebase版本同步,导致引入了高版本TypeScript语法。在package.json中添加resolutions字段强制锁定这些包的版本:
"resolutions": { "@firebase/auth-compat": "9.4.1", "@firebase/database-compat": "9.4.1", "@firebase/storage-compat": "9.4.1", // 项目中用到的其他firebase-compat包都需要添加 }
npm用户:需要安装
npm-force-resolutions作为开发依赖,并添加前置脚本:npm install --save-dev npm-force-resolutions在
package.json的scripts中加入:"preinstall": "npx npm-force-resolutions"然后执行
npm install重新安装依赖。Yarn用户:直接执行
yarn install即可自动应用resolutions配置。
2. 调整TypeScript编译配置
修改tsconfig.json的compilerOptions,确保兼容Angular 12对应的TypeScript 4.3.x版本语法:
"compilerOptions": { "importsNotUsedAsValues": "remove", "preserveValueImports": false, // 保留原有其他配置 }
3. 清理缓存并重新构建
彻底清理依赖和缓存,避免残留的旧文件干扰:
# 删除依赖和锁文件 rm -rf node_modules package-lock.json yarn.lock # 重新安装依赖 npm install # 或 yarn install # 清理Angular缓存 ng cache clean # 重新构建 ng build
内容的提问来源于stack exchange,提问作者Arun Kumar

