Skip to content

Specify the return document type for AngularFirestoreDocument.get() #2361

@zer09

Description

@zer09

The return type of the AngularFirestoreDocument.get() is not set, this will cause the editor cant perform autocomplete when calling DocumentSnapshot.data() because it will just treat its return type, as firestore.DocumentData instead the real type.

Version info

Angular: 9.0.7

Firebase: 7.11.0

AngularFire: 5.4.2

Other (e.g. Ionic/Cordova, Node, browser, operating system):
Node: v12.6
npm: 6.14.2
Browser: Latest chrome and firefox nightly.

How to reproduce these conditions

export class Sample {
  constructor(private afStore AngularFirestore) {}
  
  test(path: string)  {
    this.afStore.doc<SampleType>(path).get().pipe(take(1)).subscribe(doc => {
      console.log(doc.data()); // The real type of doc.data() is not infered by the editor.
    })
  }
}

Expected behavior

personalinfo

The return type should be

Observable<firebase.firestore.DocumentSnapshot<firebase.firestore.SamlpeType>>

This will allow to infer the type of the doc.data()

Actual behavior

documentdata

The return type is

Observable<firebase.firestore.DocumentSnapshot<firebase.firestore.DocumentData>>

With this the autocomplete will not work

Possible Solution

Based on index.d.ts on firestore, it is implemented like this.

export class DocumentSnapshot<T = DocumentData> {
  data(options?: SnapshotOptions): T | undefined;
}

I think setting the return type of get on document.ts to

get(options?: firestore.GetOptions): Observable<firestore.DocumentSnapshot<T>>

will fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions