Hi,
First of all: thanks for this 😎 library. I'm having a quick typescript question:
setQueryData takes an updater in the form of:
((oldData: T | undefined) => T)
The question is: if oldData is possibly undefined, how would be we able to construct T?
suppose we have a simple type:
type Person = {
firstName: string,
lastName: string
}
and we mutate that persons firstName, which we want to optimistically update (I've taken the optimistic update example as a base):
useMutation(
firstName => axios.patch('/api/person', { firstName }),
{
onMutate: firstName => {
queryCache.cancelQueries('person')
const previousValue = queryCache.getQueryData('person')
queryCache.setQueryData('person', old => ({
...old,
firstName
}))
return previousValue
}
)
but if oldData is possibly undefined, the resulting object can never be of type Person - it might miss the lastName.
We are currently working around this by doing:
const previousValue = queryCache.getQueryData('person')
if (previousValue) {
queryCache.setQueryData('person', {
...previousValue,
firstName
})
}
The question is: can old really be undefined ? If so, wouldn't this be a runtime error in the example here when accessing old.items ? Or is this maybe just an issue with the type definitions and old can actually never be undefined?
Thanks for clarifying
Hi,
First of all: thanks for this 😎 library. I'm having a quick typescript question:
setQueryDatatakes anupdaterin the form of:The question is: if
oldDatais possibly undefined, how would be we able to constructT?suppose we have a simple type:
and we mutate that persons firstName, which we want to optimistically update (I've taken the optimistic update example as a base):
but if
oldDatais possiblyundefined, the resulting object can never be of typePerson- it might miss thelastName.We are currently working around this by doing:
The question is: can
oldreally beundefined? If so, wouldn't this be a runtime error in the example here when accessingold.items? Or is this maybe just an issue with the type definitions andoldcan actually never be undefined?Thanks for clarifying