Search code examples
angularfirebasegoogle-cloud-firestoreimport

Issue with importing AngularFirestoreModule: "This type parameter might need an `extends firebase.firestore.DocumentData` constraint."


Hi I have a problem with connecting Firebase (Firestore) to my Angular App. Thats the first time I am trying to connect firebase, so i followed a tutorial from Google. https://developers.google.com/codelabs/building-a-web-app-with-angular-and-firebase#10

In step 11 I have to import AngularFirestoreModule, which I did:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MapComponent } from './map/map.component';
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import { NavbarComponent } from './navbar/navbar.component';
import { CodeComponent } from './navbar/code/code.component';
import { ManagerComponent } from './manager/manager.component';
import { initializeApp,provideFirebaseApp } from '@angular/fire/app';
import { environment } from '../environments/environment';
import { provideFirestore,getFirestore } from '@angular/fire/firestore';
import {AngularFireModule} from "@angular/fire/compat";
import {AngularFirestoreModule} from "@angular/fire/compat/firestore";

@NgModule({
  declarations: [
    AppComponent,
    MapComponent,
    NavbarComponent,
    CodeComponent,
    ManagerComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideFirestore(() => getFirestore()),
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule
  ],
  providers: [
    ManagerComponent,
    CodeComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

After importing I got following issues:

Error: node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:18 - error TS2430: Interface 'DocumentSnapshotExists<T>' incorrectly extends interface 'DocumentSnapshot<DocumentData>'.
  The types returned by 'data(...)' are incompatible between these types.
    Type 'T' is not assignable to type 'DocumentData | undefined'.
      Type 'T' is not assignable to type 'DocumentData'.

13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
                    ~~~~~~~~~~~~~~~~~~~~~~

  node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
    13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
                                               ~
    This type parameter might need an `extends firebase.firestore.DocumentData` constraint.
  node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
    13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
                                               ~
    This type parameter might need an `extends firebase.firestore.DocumentData | undefined` constraint.


Error: node_modules/@angular/fire/compat/firestore/interfaces.d.ts:23:18 - error TS2430: Interface 'QueryDocumentSnapshot<T>' incorrectly extends interface 'QueryDocumentSnapshot<DocumentData>'.
  The types returned by 'data(...)' are incompatible between these types.
    Type 'T' is not assignable to type 'DocumentData'.

  node_modules/@angular/fire/compat/firestore/interfaces.d.ts:29:33
    29 export interface DocumentChange<T> extends firebase.firestore.DocumentChange {
                                       ~
    This type parameter might need an `extends firebase.firestore.DocumentData` constraint.




× Failed to compile.

I dont know how to fix my problem or how I could connect my Firestore Database in another way. Thank you for helping!

Here also my app.component.ts:

import { Component } from '@angular/core';
import {AngularFirestore} from "@angular/fire/compat/firestore";
import {Observable} from "rxjs";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  toMap:boolean = true;
  items: Observable<any[]>;

  constructor(firestore: AngularFirestore) {
    this.items = firestore.collection('items').valueChanges();
  }

  switchToMap(){
    this.toMap = !this.toMap;
  }
}

I tried to follow a tutorial from Google, but got the problem, which is descriped in the upper field. https://developers.google.com/codelabs/building-a-web-app-with-angular-and-firebase#10


Solution

  • I had the same issue and this Github comment fixed it for me: https://github.com/angular/angularfire/issues/3290#issuecomment-1323837275

    In your node_modules/@angular/fire/compat/firestore/interfaces.d.ts, add a generic <T> to the end of the interfaces where the errors are.

    To start, update this line from this:

    export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot 
    

    to this:

    export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot<T> 
    

    Do this for all of them.

    enter image description here