TanStackT
TanStack2y ago
4 replies
conventional-black

query data type is (Type | undefined) after `isError` check

const getVisitById = async (id: number): Promise<VisitResponse> => {
  return await GET(`/visits/${id}`);
};


const { data, isError, error } = useQuery({
    queryKey: ['visit', 18657],
    queryFn: () => getVisitById(18657),
  });

  if (isError) {
    return <div>{error.message}</div>;
  }

  console.log(data)


I expect
data
to be type of VisitResponse after isError check, but it is VisitResponse | undefined

If I do the following I get the type of VisitResponse for
data


  if (isError) {
    return <div>{error.message}</div>;
  }

  if (!isSuccess) return

  console.log(data)


Is there way to change this behaviour so that checking only isError would suffice?
Was this page helpful?