question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect type definitions in hooks?

See original GitHub issue

Seems 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

Test case on Code Sandbox

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

  1. Visit https://codesandbox.io/s/reactfire-typescript-wrong-types-4wq4o
  2. Open the file src/withoutOptions.ts
  3. Right-click useFirestoreDoc and select “Go To Definition”"
  4. Check if the firebase/app import is failing. If it is, replace with just firebase. Don’t save the file, but leave it open (important).
  5. Go back to src/withoutOptions.ts and check the type of doc 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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
codediodeiocommented, Oct 16, 2019

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:

interface ReactfireCollection<T> {
  snapshot?: firestore.QuerySnapshot,
  data?: T[]
}

// Usage would look like this:

const { snapshot, data } = useFirestoreCollection<Dog>(ref);
const { snapshot: catSnap, data: catData, } = useFirestoreCollection<Cat>(ref);

Benefits:

  1. Eliminates the need for the extra data hooks.
  2. Easy to strongly type. Data would take initialValue, so you don’t need union types at all.
1reaction
jamesdanielscommented, Feb 26, 2020

Yeah, I’m calling this one good. Open a new issue for any type problems you experience as of the latest RCs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React & TypeScript: how to type hooks (a complete guide)
Hooks are a fundamental aspect of writing React components, so knowing how to type them is vital to using TypeScript with React.
Read more >
Typescript error type not assign able to state - usestate
First, we should check if there is an interface mismatch between State , and the initialState object. It will help if you can...
Read more >
The React TypeScript Cheatsheet – How To Set Up Types ...
In this guide, I will show you how to set up TypeScript types on React hooks (useState, useContext, useCallback, and so on).
Read more >
Hooks | React TypeScript Cheatsheets
Type inference works very well for simple values: const [state, setState] = useState(false); // `state` is inferred to be a boolean. // `setState`...
Read more >
Rules of Hooks
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. Hooks are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found