T
TanStack8mo ago
optimistic-gold

Wrapper around injectQuery

Has anyone been able to create a wrapper around injectQuery? I want to extend the query so that it has some more methods. Doing a simple shallow copy of the query works well:
const q = injectQuery(optionsFn)
const extendedQuery = {
...q,
}

if (extendedQuery.isSuccess()) {
extendedQuery.data() // T
}
const q = injectQuery(optionsFn)
const extendedQuery = {
...q,
}

if (extendedQuery.isSuccess()) {
extendedQuery.data() // T
}
But as soon as I add a simple method, the type predicates just seems to be broken:
const qs = injectQuery(optionsFn)
const extendedQuery = {
...q,
test: () => true
}

if (extendedQuery.isSuccess()) {
extendedQuery.data() // T | undefined
}
const qs = injectQuery(optionsFn)
const extendedQuery = {
...q,
test: () => true
}

if (extendedQuery.isSuccess()) {
extendedQuery.data() // T | undefined
}
2 Replies
generous-apricot
generous-apricot8mo ago
Wrappers and maintaining good typing are indeed a challenge. What are you trying to achieve with the extra methods? Maybe there's a better way.
optimistic-gold
optimistic-goldOP7mo ago
I successfully did it, it was a mess but I end up getting something that I’m happy with 😂 I’ll explain it tomorrow, I will go to sleep now.

Did you find this page helpful?