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.

UseCollectionData wrong type (TS2345)

See original GitHub issue

Hello, now with the new version i get the following error - in V2.2.0 it works:

TS2345: Argument of type ‘DocumentReference<DocumentData>’ is not assignable to parameter of type ‘Query<FirebaseCourse>’.   Type ‘DocumentReference<DocumentData>’ is missing the following properties from type ‘Query<FirebaseCourse>’: where, orderBy, limit, limitToLast, and 4 more.

code:

const [coursesData] = useCollectionData<FirebaseCourseWithUid>(
    firebase.firestore().collection('courses'),
    { idField: 'uid' }
  );

FirebaseCourseWithUid:

export type FirebaseCourseWithUid = {
  title: string;
  description: string;
  start: string;
  duration: number;
  recurrence: { daysOfWeek: Array<number>; weekInterval: number } | null;
  trainerId: string;
  imageUrl: string;
  isOnline: boolean;
  uid: string;
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dangreavescommented, Mar 9, 2021

I’m having the same issue since upgrading to v3. Here’s a simple example.

interface Task {
  title: string,
  message: string
}

const [tasks, loading, error] = useCollectionData<Task>(
  firebase
    .firestore()
    .collection("tasks")
);

This throws the following type error.

Argument of type 'CollectionReference<DocumentData>' is not assignable to parameter of type 'Query<Task>'.
  The types returned by 'where(...)' are incompatible between these types.
    Type 'Query<DocumentData>' is not assignable to type 'Query<Task>'.
      Type 'DocumentData' is missing the following properties from type 'Task': title, message

It seems that removing the generic from firebase.firestore.Query does the trick, but I’m not sure what type features this then loses. I’m guessing that generic perhaps adds types into the where() method, but unsure.

https://github.com/CSFrequency/react-firebase-hooks/blob/master/firestore/useCollection.ts#L34

export const useCollectionData = <
  T = firebase.firestore.DocumentData,
  IDField extends string = '',
  RefField extends string = ''
>(
-  query?: firebase.firestore.Query<T> | null,
+  query?: firebase.firestore.Query | null,
  options?: DataOptions<T>
): CollectionDataHook<T, IDField, RefField> => {
  return useCollectionDataInternal(true, query, options);
};
0reactions
app-akrobatencommented, Mar 10, 2021

Hey. Thank you for your quick reply. Im also on firebase@8.2.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

Argument of type '"==="' is not assignable to parameter of type ...
But the program won't compile and gives the following error: ... error TS2345: Argument of type '"==="' is not assignable to parameter of ......
Read more >
TypeScript – When a data type is not assignable to itself ...
src/plugin/visitors/flow.ts:59:22 - error TS2345: Argument of type 'TSTypeAliasDeclaration' is not assignable to parameter of type 'AnyTypeAnnotation ...
Read more >
react-firebase-hooks - npm
A set of reusable React Hooks for Firebase. npm version npm downloads. This documentation is for v5 of React Firebase Hooks which requires ......
Read more >
React bindings for Firebase | Authentication library - kandi
useCollectionData returns undefined in react-firebase-hooks ... Firebase onSnapshot in react error, "Expected type 'bc', but it was: a custom he ...
Read more >
Using Firestore With Typescript - Medium
What if I get the type wrong? More code to write etc. etc. Something else that gives me the chills is having to...
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