firebase / firebase/firebase-js-sdk

Firestore: missing generics for `doc`, `collection`, `collectionGroup`, and `withConverter`

Open
#6,961 3 comments 2 reactions 1 assignee Claimed by @MarkDuckworth View on GitHub
api: firestore v9
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### [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` or `as CollectionReference`). It is not possible to set the return value of these functions directly to a variable of type `DocumentReference` or `CollectionReference`. 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:

```typescript
// Type 'CollectionReference' is not assignable to type 'CollectionReference
const collRef: CollectionReference = collection(firestore, 'countries');
// Type 'CollectionReference' is not assignable to type 'CollectionReference
const collRef2: CollectionReference = collection(firestore, 'countries').withConverter(null);
// Type 'CollectionReference' is not assignable to type 'CollectionReference
const collRef3: CollectionReference = collection(collRef, 'germany', 'cities');

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

// Type 'Query' is not assignable to type 'Query
const query: Query = 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

- #4308

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.