Firebase项目出现Auth-compat index.d.ts TS1005语法错误求助
错误信息
Error: node_modules/@firebase/auth-compat/dist/auth-compat/index.d.ts:53:421 - error TS1005: ',' expected.
53 import { type User, type Unsubscribe, type ActionCodeInfo, type UserCredential, type Auth, type IdTokenResult, type MultiFactorError, type MultiFactorResolver, type PopupRedirectResolver, type Dependencies, type AuthCredential, type ApplicationVerifier, type ConfirmationResult, type AuthProvider, type MultiFactorUser, type NextOrObserver, type ErrorFn, type CompleteFn, type ActionCodeSettings, type Persistence, type PhoneAuthCredential } from "@firebase/auth";
解决方案
清理依赖并重新安装
删除依赖目录和锁文件,重新拉取完整依赖:# npm 用户 rm -rf node_modules package-lock.json npm install # yarn 用户 rm -rf node_modules yarn.lock yarn install依赖安装过程中可能出现文件损坏,导致TypeScript解析语法时出错,重新安装可解决这类问题。
统一Firebase Auth相关包版本
确保@firebase/auth和@firebase/auth-compat的版本完全一致,这两个包是配套发布的,版本不匹配会导致类型定义文件语法冲突。在package.json中指定相同版本后重新安装,例如:{ "@firebase/auth": "^9.23.0", "@firebase/auth-compat": "^9.23.0" }升级TypeScript到兼容版本
Firebase v9+ 要求TypeScript版本至少为4.5,建议升级到4.8以上的稳定版,避免语法支持问题(比如type导入语法的兼容性):npm install typescript@latest --save-dev清除TypeScript缓存
TypeScript会缓存编译结果,缓存损坏可能导致错误残留:- 删除项目中的
.tsbuildinfo文件(如果存在) - 在VSCode中按
Ctrl+Shift+P,选择「TypeScript: Restart TS Server」重启服务
- 删除项目中的
强制统一依赖版本(解决冲突)
如果项目中存在多个版本的@firebase/auth,可以通过包管理器的强制覆盖功能统一版本:- npm 8+:在
package.json中添加overrides字段"overrides": { "@firebase/auth": "9.23.0" } - Yarn:在
package.json中添加resolutions字段"resolutions": { "@firebase/auth": "9.23.0" }
添加后重新安装依赖即可。
- npm 8+:在
内容的提问来源于stack exchange,提问作者prawlercode

