I am following react 18 intermediate and I was refactoring from useData to react query, in section refactoring usePlatforms I got this issue.
usePlatforms.tsx
import { useQuery } from "@tanstack/react-query";
import platforms from "../data/platforms";
import platformServices, { Platform } from "../services/platformServices";
import { FetchResponse } from "../services/api-client";
const usePlatforms = () => useQuery<FetchResponse<Platform>>({
queryKey: ["platforms"],
queryFn: platformServices,
staleTime: 24 * 60 * 60 * 1000, // 24 hours
initialData: { count: platforms.length, results: platforms }
});
export default usePlatforms;
platformServices.tsx
import { AxiosResponse } from "axios";
import apiClient, { FetchResponse } from "./api-client";
export interface Platform {
id: string | number;
name: string;
slug: string;
platforms: Platform[];
}
const platformServices = () => apiClient.get<FetchResponse<Platform>, AxiosResponse>('/platforms/lists/parents').then(res => res.data);
export default platformServices;
error I get
src/hooks/usePlatforms.ts:7:10 - error TS2769: No overload matches this call.
Overload 1 of 3, '(options: UndefinedInitialDataOptions<FetchResponse<Platform>, Error, FetchResponse<Platform>, QueryKey>, queryClient?: QueryClient | undefined): UseQueryResult<...>', gave the following error.
Type '{ count: number; results: { id: number; name: string; slug: string; platforms: ({ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { ...; })[]; }[]; }' is not assignable to type 'undefined'.
Overload 2 of 3, '(options: DefinedInitialDataOptions<FetchResponse<Platform>, Error, FetchResponse<Platform>, QueryKey>, queryClient?: QueryClient | undefined): DefinedUseQueryResult<...>', gave the following error.
Type '{ id: number; name: string; slug: string; platforms: ({ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { id: number; ... 6 more ...; year_end: null; })[]; }[]' is not assignable to type 'Platform[]'.
Type '{ id: number; name: string; slug: string; platforms: ({ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { id: number; ... 6 more ...; year_end: null; })[]; }' is not assignable to type 'Platform'.
Types of property 'platforms' are incompatible.
Type '({ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: null; year_end: null; })[]' is not assignable to type 'Platform[]'.
Type '{ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: null; year_end: null; }' is not assignable to type 'Platform'.
Property 'platforms' is missing in type '{ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; }' but required in type 'Platform'.
Overload 3 of 3, '(options: UseQueryOptions<FetchResponse<Platform>, Error, FetchResponse<Platform>, QueryKey>, queryClient?: QueryClient | undefined): UseQueryResult<...>', gave the following error.
Type '{ id: number; name: string; slug: string; platforms: ({ id: number; name: string; slug: string; games_count: number; image_background: string; image: null; year_start: number; year_end: null; } | { id: number; ... 6 more ...; year_end: null; })[]; }[]' is not assignable to type 'Platform[]'.