rxdb-hooks icon indicating copy to clipboard operation
rxdb-hooks copied to clipboard

TypeScript + useRxCollection() + static methods not working?

Open GRardB opened this issue 2 years ago • 1 comments

Hey there!

I've got a collection that looks like this:

type EventCollection = RxCollection<
  EventRecordType,
  Record<string, never>,
  EventCollectionStaticMethods
>;

It has some static methods, but if I call useRxCollection(), there's no good way of TypeScript knowing that. The hook only accepts a single generic which is for the document type (i.e. RxCollection<T>), so I can't provide the types for my static methods.

I think it would make more sense if the generic was for the collection itself, rather than the document type, e.g.:

const events = useRxCollection<EventCollection>("events")

I realize that's a breaking change, though, so perhaps it would be enough for the function to accept all three generic types? That way, I could at least do this:

declare function useRxCollection<A, B, C>(
  name: string,
): RxCollection<A, B, C> | null;

...

const events: EventCollection = useRxCollection("events")

Right now, that line of code fails because the second and third generics aren't being passed through.

I'm happy to open a PR if you want, though I think the change would be very quick.

Thanks!

GRardB avatar Dec 29 '23 21:12 GRardB

+1 similarly, if you define ORM methods on your collection, it doesn't have a way of recognizing that the result documents have those methods available

jb-chief avatar Aug 21 '24 20:08 jb-chief