cvara / rxdb-hooks

React hooks for integrating with RxDB
MIT License
132 stars 5 forks source link

Database type doesn't match type expected by Provider #40

Closed nirvdrum closed 2 years ago

nirvdrum commented 2 years ago

I'm just getting started with RxDB and have run into a very similar issue to #14. Given how much RxDB and its TypeScript support has changed since then, I can't say for certain that they're the same issue. If they are, please feel free to merge the issues.

Following the RxDB TypeScript docs, the createRxDatabase call will return a typed instance of a specific database (e.g., MyDatabase in the linked page). This type is not equivalent to RxDatabase and as such, can't be supplied as the db prop on rxdb-hook's Provider. Instead, you get a type error like:

Type 'MyDatabase' is not assignable to type 'RxDatabase<CollectionsOfDatabase, any, any> | undefined'.

I've worked around it locally by just ignoring the type:

<Provider db={db as any}>{props.children}</Provider>

That's enough to get most of the hooks working. However, the useRxDB hook will not return a reference typed as MyDatabase, so you can't do much with the reference without type casting.

cvara commented 2 years ago

Thanks for reporting this @nirvdrum. There is indeed an issue there caused by how Typescript tries to narrow inferred types, which is also not specific to RxDB 11. It will be fixed by #41, but I will have to settle on a travis ci alternative before merging, so it's not 100% ready yet.

cvara commented 2 years ago

Issue should be fixed in 4.0.1.