Skip to content

Firestore: missing generics for doc, collection, collectionGroup, and withConverter #6961

@andipaetzold

Description

@andipaetzold

[REQUIRED] Describe your environment

  • Operating System version: all
  • Browser version: all
  • Firebase SDK version: 9.16.0
  • Firebase Product: firestore (auth, database, storage, etc)

[REQUIRED] Describe the problem

When using the firebase/firestore with TypeScript, some function signatures of doc, collection, collectionGroup and withConverter do not include TypeScript generics and are therefore complicated to use.

For some signatures of these functions, the data type of the returned references is always DocumentData and can only be changed by using a type assignment (as DocumentReference<DocumentData> or as CollectionReference<DocumentData>). It is not possible to set the return value of these functions directly to a variable of type DocumentReference<Country> or CollectionReference<Country>. You always have to use a type assignment.

It should be possible to not use a type assignment here. Especially because it's possible with some of the signatures already.

Steps to reproduce:

Use the code below and you will see type errors.

Relevant Code:

// Type 'CollectionReference<DocumentData>' is not assignable to type 'CollectionReference<Country>
const collRef: CollectionReference<Country> = collection(firestore, 'countries');
// Type 'CollectionReference<DocumentData>' is not assignable to type 'CollectionReference<Country>
const collRef2: CollectionReference<Country> = collection(firestore, 'countries').withConverter(null);
// Type 'CollectionReference<DocumentData>' is not assignable to type 'CollectionReference<City>
const collRef3: CollectionReference<City> = collection(collRef, 'germany', 'cities');

// Type 'DocumentReference<DocumentData>' is not assignable to type 'DocumentReference<Country>
const docRef: DocumentReference<Country> = doc(firestore, "countries",  "germany");
// Type 'DocumentReference<DocumentData>' is not assignable to type 'DocumentReference<Country>
const docRef2: DocumentReference<Country> = docRef.withConverter(null);
// Type 'DocumentReference<DocumentData>' is not assignable to type 'DocumentReference<City>
const docRef3: DocumentReference<Country> = doc(docRef2, 'cities', 'berlin');

// Type 'Query<DocumentData>' is not assignable to type 'Query<City>
const query: Query<City> = collectionGroup(firestore, "cities");

I already forked the repo and fixed these issues locally. I am happy to open a PR and provide the required changes to fix this bug.
I didn't just want to throw code at you without prior announcement, that's why I opened this issue first. Also, I want to double-check whether this might be desired behavior e.g. to guide people toward using data converters. To me, the code above seems to be obvious happy paths, so I am surprised they do not work properly.
Or maybe you already attempted a fix and it's not as simple as I think.

Please let me know if I should open the PR.

Related Issues/PRs

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions