T
TanStack2w ago
adverse-sapphire

Infer result of select

const myQuery = useQuery({
select: data => 1
})

// assume i have to write a function like this:
function handleQueryData(data: WhatDoIPutHere) {
// ...
}
const myQuery = useQuery({
select: data => 1
})

// assume i have to write a function like this:
function handleQueryData(data: WhatDoIPutHere) {
// ...
}
(where WhatDoIPutHere becomes number)
7 Replies
correct-apricot
correct-apricot2w ago
Lucky for you, Dominik just wrote an in depth blog on this https://tkdodo.eu/blog/react-query-selectors-supercharged
React Query Selectors, Supercharged
How to get the most out of select, sprinkled with some TypeScript tips.
adverse-sapphire
adverse-sapphireOP2w ago
i read the whole article but i don't think it covers inferring the return type of an inline function passed to select. only how to abstract the function itself as a parameter.
genetic-orange
genetic-orange2w ago
WhatDoIPutHere is whatever the queryFn returns, as that is the input for select. It gets inferred if you inline it
adverse-sapphire
adverse-sapphireOP2w ago
i should have named the function "handleSelectedQueryData". what if i have many inline select functions and still need to get their return type?
genetic-orange
genetic-orange2w ago
Please show a typescript playground

Did you find this page helpful?