You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular构建报错TS2300:@firebase/auth-compat中'type'标识符重复

解决Angular构建时Firebase compat包的TS2300重复标识符错误

执行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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 09:15:59