I am using a queryFn inside an API call using rtk-query and I cant figure out why typescript is complaining about the return value of the queryFn -- any help would be much appreciated.
This is a simplified version of my code
type Fields = {
name: string;
dob: string;
}
const fieldsApi = api.injectEndpoints({
endpoints: (build) => ({
getFields: build.query<{ data: Fields[] }, void>({
queryFn(arg, api, _, baseQuery) {
const { featureFlags } = api.getState() as RootState;
if (!featureFlags?.fieldsEnabled) {
return Promise.resolve([]);
}
return baseQuery({ url: "/fields" });
},
})
})
})
We need to bail out of requests if the featureFlag is not set (this works) and the calls to get the data are working correctly -- but typescript is not happy.
Posting the error below
Type '(arg: void, api: BaseQueryApi, _: {}, baseQuery: (arg: string | FetchArgs) => MaybePromise<QueryReturnValue<unknown, FetchBaseQueryError, FetchBaseQueryMeta>>) => MaybePromise<...> | Promise<...>' is not assignable to type '(arg: void, api: BaseQueryApi, extraOptions: {}, baseQuery: (arg: string | FetchArgs) => MaybePromise<QueryReturnValue<unknown, FetchBaseQueryError, FetchBaseQueryMeta>>) => MaybePromise<...>'.
Type 'MaybePromise<QueryReturnValue<unknown, FetchBaseQueryError, FetchBaseQueryMeta>> | Promise<never[]>' is not assignable to type 'MaybePromise<QueryReturnValue<{ data: Fields[]; }, FetchBaseQueryError, unknown>>'.
Type '{ error?: undefined; data: unknown; meta?: FetchBaseQueryMeta | undefined; }' is not assignable to type 'MaybePromise<QueryReturnValue<{ data: Fields[]; }, FetchBaseQueryError, unknown>>'.
Type '{ error?: undefined; data: unknown; meta?: FetchBaseQueryMeta | undefined; }' is not assignable to type '{ error?: undefined; data: { data: Fields[]; }; meta?: unknown; }'.
Types of property 'data' are incompatible.
Type 'unknown' is not assignable to type '{ data: Fields[]; }'.ts(2322)
Do I need to do an as cast when using the queryFn or something like that?