我试图在一个新的角度项目中添加angularfire数据库模块,但是当我添加这一行时:
import { AngularFireDatabaseModule } from '@angular/fire/compat/database';
我知道这个错误:
Error: node_modules/@angular/fire/compat/database/interfaces.d.ts:47:18 - error TS2430: Interface 'DatabaseSnapshotExists<T>' incorrectly extends interface 'DataSnapshot'.
Types of property 'forEach' are incompatible.
Type '(action: (a: DatabaseSnapshot<T>) => boolean) => boolean' is not assignable to type '(action: (a: DataSnapshot & { key: string; }) => boolean | void) => boolean'.
Types of parameters 'action' and 'action' are incompatible.
Types of parameters 'a' and 'a' are incompatible.
Type 'DatabaseSnapshot<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotExists<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotExists<T>' is not assignable to type 'DataSnapshot'.
Types of property 'forEach' are incompatible.
Type '(action: (a: DatabaseSnapshot<T>) => boolean) => boolean' is not assignable to type '(action: (a: DataSnapshot & { key: string; }) => boolean | void) => boolean'.
Types of parameters 'action' and 'action' are incompatible.
Types of parameters 'a' and 'a' are incompatible.
Type 'DatabaseSnapshot<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotDoesNotExist<T>' is not assignable to type 'DataSnapshot &
{ key: string; }'.
Type 'DatabaseSnapshotDoesNotExist<T>' is not assignable to type '{ key: string; }'.
Types of property 'key' are incompatible.
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
47 export interface DatabaseSnapshotExists<T> extends firebase.database.DataSnapshot {
~~~~~~~~~~~~~~~~~~~~~~
Error: node_modules/@angular/fire/compat/database/interfaces.d.ts:52:18 - error TS2430: Interface 'DatabaseSnapshotDoesNotExist<T>' incorrectly extends interface 'DataSnapshot'.
Types of property 'forEach' are incompatible.
Type '(action: (a: DatabaseSnapshot<T>) => boolean) => boolean' is not assignable to type '(action: (a: DataSnapshot & { key: string; }) => boolean | void) => boolean'.
Types of parameters 'action' and 'action' are incompatible.
Types of parameters 'a' and 'a' are incompatible.
Type 'DatabaseSnapshot<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotDoesNotExist<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotDoesNotExist<T>' is not assignable to type 'DataSnapshot'.
Types of property 'forEach' are incompatible.
Type '(action: (a: DatabaseSnapshot<T>) => boolean) => boolean' is not assignable to type '(action: (a: DataSnapshot & { key: string; }) => boolean | void) => boolean'.
Types of parameters 'action' and 'action' are incompatible.
Types of parameters 'a' and 'a' are incompatible.
Type 'DatabaseSnapshot<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotExists<T>' is not assignable to type 'DataSnapshot & { key: string; }'.
Type 'DatabaseSnapshotExists<T>' is not assignable to type '{ key: string; }'.
Types of property 'key' are incompatible.
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
52 export interface DatabaseSnapshotDoesNotExist<T> extends firebase.database.DataSnapshot {
以下是我完整的app.module.ts文件:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import {AngularFireModule} from '@angular/fire/compat';
import { AngularFireDatabaseModule } from '@angular/fire/compat/database';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
BrowserAnimationsModule,
],
providers: [ ],
bootstrap: [AppComponent]
})
export class AppModule { }
我已经尝试删除和重新安装软件包,但这是行不通的。另外,我有一个老的项目,它有完全相同的代码行和相同版本的角和角/火(“@角/核心”:"^14.0.0“、”@角/火“:"^7.4.1"),但我无法创建一个新的项目。
有什么想法吗?
发布于 2022-08-09 09:23:10
原因
造成此错误的原因是firebase@9.9.2中firebase.database.DataSnapshot的firebase.database.DataSnapshot签名发生了变化。在您的旧项目中,您可能有一个较旧的版本(您可以签入Packy-lock.json或在您的node_modules中),如果您在那里运行npm update --save
,就会出现错误。
这些是来自“角/火”的接口:
export interface DatabaseSnapshotExists<T> extends firebase.database.DataSnapshot {
exists(): true;
val(): T;
forEach(action: (a: DatabaseSnapshot<T>) => boolean): boolean;
}
export interface DatabaseSnapshotDoesNotExist<T> extends firebase.database.DataSnapshot {
exists(): false;
val(): null;
forEach(action: (a: DatabaseSnapshot<T>) => boolean): boolean;
}
在firebase@9.9.2中,firebase.database.DataSnapshot有这样的方法:
forEach(
action: (
a: firebase.database.DataSnapshot & { key: string }
) => boolean | void
): boolean;
在firebase@9.9.1中:
forEach(
action: (a: firebase.database.DataSnapshot) => boolean | void
): boolean;
解决方案
您可以显式安装firebase@9.9.1以消除此错误(npm install firebase@9.9.1 --save --save-exact
)。但是,请记住,当此问题得到解决时,稍后要更新:https://github.com/angular/angularfire/issues/3255
更新
这个问题用firebase@9.9.3解决了。firebase.database.DataSnapshot
具有与firebase@9.9.1中相同的forEach
签名。所以,只需使用firebase@9.9.3 (或更新版本)或从package.json
中删除它,并使用由@angular/fire
获取的版本。
https://stackoverflow.com/questions/73281199
复制相似问题