Incorrect type definitions in hooks?
See original GitHub issueSeems like the type definitions for useFirestoreDoc
and useFirestoreCollection
are incorrect:
export declare function useFirestoreDoc<T = unknown>(ref: firestore.DocumentReference, options?: ReactFireOptions<T>): firestore.DocumentSnapshot | T;
In a union, unknown
absorbs everything, so if we don’t pass a generic type here the return type will always be unknown
. I tried to find a way to fix this so I could do a PR myself, but I’m afraid my TypeScript knowledge is lacking.
If I understand correctly, what we want here is to make the return type be firestore.DocumentSnapshot
by default, or T
if specified. I opened a PR with a way to do this but I’m really not sure if this is how it’s supposed to be done.
Version info
React: 16.8.6
Firebase: 6.0.4
ReactFire: 2.0.0-canary.fc2be5b
Typescript: 3.5.3 (on my machine) and 3.3.3 (on CodeSandox) both show the same problem
Test case
I didn’t try to make the project actually run, because the issue is just the type definitions. The issue isn’t visible right away, though, because reactfire can’t import from firebase/app
on Code Sandbox for some reason. Follow these steps to see the problem:
Steps to reproduce
- Visit https://codesandbox.io/s/reactfire-typescript-wrong-types-4wq4o
- Open the file
src/withoutOptions.ts
- Right-click
useFirestoreDoc
and select “Go To Definition”" - Check if the
firebase/app
import is failing. If it is, replace with justfirebase
. Don’t save the file, but leave it open (important). - Go back to
src/withoutOptions.ts
and check the type ofdoc
by hovering the mouse over it.
Expected behavior
The type should be firestore.DocumentSnapshot
by default
Actual behavior
The type is unknown
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Here’s another option.
It does not make much sense to use a generic because
<Collection|Document>Snapshot
does not take a generic to begin with. TBH, I’d rather not have a union type and just manually cast the type to the data.OR
The hook could return an object with the
QuerySnapshot
in addition to the data cast to the generic type, for example:Benefits:
data
hooks.Yeah, I’m calling this one good. Open a new issue for any type problems you experience as of the latest RCs.