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

导入@angular/fire/firestore时遇TS2314错误求助

解决Generic type 'AggregateQuerySnapshot<T>' requires 1 type argument(s)错误

问题背景

导入@angular/fire/firestore时触发TypeScript错误:

Error: node_modules/rxfire/firestore/lite/interfaces.d.ts:8:29 - error TS2314: Generic type 'AggregateQuerySnapshot' requires 1 type argument(s).

使用版本:firebase 9.23.0,@angular/fire 7.6.1

项目核心代码:

import { Injectable, inject } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/compat/auth';
import { getAuth, signInWithEmailAndPassword, 
         createUserWithEmailAndPassword, updateProfile } from 
         'firebase/auth';
import { User } from '../models/user.model';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { getFirestore, setDoc, doc } from 
         '@angular/fire/firestore';


export class FirebaseService {
auth = inject(AngularFireAuth);
firestore = inject(AngularFirestore);

setDocument(path: string, data: any) {
return setDoc(doc(getFirestore(), path), data);
}
}

错误原因

该错误源于依赖版本不兼容:@angular/fire 7.6.1依赖的rxfire版本与firebase 9.23.0的类型定义冲突——Firebase 9.23.0调整了AggregateQuerySnapshot的泛型参数要求,但旧版rxfire(被@angular/fire 7.6.1默认拉取)未同步适配。

解决方案

方案1:降级Firebase到适配@angular/fire 7.6.1的版本

卸载当前依赖后安装匹配版本:

npm uninstall firebase @angular/fire
npm install firebase@9.22.2 @angular/fire@7.6.1

方案2:升级@angular/fire到兼容Firebase 9.23.0的版本

直接升级@angular/fire到最新7.x版本(已适配Firebase 9.23.0的类型变化):

npm install @angular/fire@latest

验证步骤

完成依赖调整后,清理缓存并重新构建项目:

rm -rf node_modules package-lock.json
npm install
ng build

内容的提问来源于stack exchange,提问作者Enrique García

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 21:17:56