-
|
Hi, I can't find a way to infer the type for the error of the Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Hi! The error type can be set like this interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}
function useTodos(): QueryResult<Todo[], Error> {
return useQuery("todos", async () => {
const response = await fetch("https://jsonplaceholder.typicode.com/todos");
if (!response.ok) {
throw new Error("Failed to fetch!");
}
return await response.json();
});
}If the function throws the Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
|
The second argument in the what I did was: interface App {} // response in "data"
interface ApiError {} // error object
const { data, error } = useQuery<App, ApiError>(['app', id], () =>
AppsService.getApiV1Apps(id)
);
|
Beta Was this translation helpful? Give feedback.
-
|
for v5, please see: https://tanstack.com/query/v5/docs/react/typescript#registering-a-global-error |
Beta Was this translation helpful? Give feedback.
Hi!
The error type can be set like this
If the function throws the
errorvariable will be of typeError, as specified inQueryResult.Hope this helps!