I am using useQuery hook from react-query and when I try to access my keys inside of query-function i.e., 2nd argument for useQuery, I am facing an issue with typescript it's complaining on this statement
const postId = queryKey[1].id as any
that
Property 'id' does not exist on type 'string | { id: string | undefined; comments: boolean; }'.
Property 'id' does not exist on type 'string'.ts(2339)
Everything works fine, I have a defined in postId, the request is sent to the server and I get a response back, problem is with typescript, I even tried to assert the type but doesn't work
const { data } = useQuery(['post', { id: post?.id, comments: true }], async ({ queryKey }) => {
const postId = queryKey[1].id as any
const { data } = await axios.get(`/post/${postId}`, {
headers: {
Authorization: `Bearer ${user?.idToken}`,
},
})
return data
})
Is there any typescript way to solve this ?