Allow Register
-ing of the QueryKey
type via module augmentation
#8495
Unanswered
neefrehman
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm working on a codebase that has a fairly straightforward hierarchy, and where generally we have the first part of a
queryKey
be the "area" of the app that theuseQuery
instance is in. Simplified, ourqueryKey
s almost always look like this:We use these "areas" to easily invalidate the relevant part of the app depending on what type of mutation has happened. As an example, we will always invalidate
["paymentsDashboard"]
if a mutation has happened in that part of the app. Despite having this structured approach to query keys,QueryKey
is typed asReadonlyArray<unknown>
, meaning we can't enforce or hint this structure across the entirety of the library's surface area, and so there is some risk of mispelling or the proliferation of queryKey prefixes that don't conform to the structure.To get around this, we can create an enum or other typed utility in user-land, to help us create
queryKey
s with the desired prefixes. But an obvious downside is that any developer would have to remember to use it. This problem also seems like something the library itself could help with, by extending the use ofRegister
to cover theQueryKey
type, as can currently happen forQueryMeta
and forDefaultError
.Exposing this ability would help solve this problem at a lower level than is possible in user-land, and with the added benefit of allowing enforcement. This TS Playground gives an example of how we could benefit from updating the types. As far as the implementation goes, I've had success patching the type locally as follows:
And using the following in a
.d.ts
file:Would be curious to hear from the maintainers if they think this is a good idea, and if so I'd be happy to open a PR. If not, I'm also keen to find out if there is a better approach!
Beta Was this translation helpful? Give feedback.
All reactions