Search code examples
reduxredux-toolkit

What is the correct way to type an rtk query BaseQueryFn with custom DefinitionExtraOptions?


I want to use custom base query, and supply it with extra options. Here's a toy example:

import { BaseQueryFn, createApi } from '@reduxjs/toolkit/query/react';

const baseQueryTest: BaseQueryFn<
  unknown,
  unknown,
  string,
  { customData: string }
> = async () => {
  throw new Error('Not implemented');
};

createApi({
  baseQuery: baseQueryTest,
  endpoints: () => ({}),
});

Unfortunately, I get this error:

Type 'BaseQueryFn<unknown, unknown, string, { customData: string; }, {}>' is not assignable to type 'BaseQueryFn<any, unknown, unknown, {}, {}>'. typescript (2322) [58, 3]
  Property 'customData' is missing in type '{}' but required in type '{ customData: string; }'. 

What am I doing wrong, and what's the right way to type this function?


Solution

  • I think at this point, DefinitionExtraOptions can only contain optional values (as extraOptions itself is always optional). If you add a ?, it works.