Search code examples
reactjstypescriptrtk-query

RTK Query UseLazyQuery trigger: how to get the correct type?


I need to get correct typing for trigger, that I'm passing to the child component, cannot find how to type it.

...      
const [trigger] = useLazyGetQuery();
...
<ChildComponent trigger={trigger} />
...

Child component where I need to type somehow RTK query trigger except any:

export const ChildComponent = ({ trigger }: { trigger: any }) => {...}

Solution

  • I found correct type:

    import { LazyQueryTrigger } from "@reduxjs/toolkit/dist/query/react/buildHooks";
    import {
      BaseQueryFn,
      FetchArgs,
      FetchBaseQueryError,
      FetchBaseQueryMeta,
      QueryDefinition,
    } from "@reduxjs/toolkit/query";
    import { IDemoRequest } from "../../../../../../../modules/Demo/apis/interfaces/IDemoRequest";
    
    export type LazyGetTriggerType = LazyQueryTrigger<
      QueryDefinition<
        IDemoRequest,            // Request parameters type
        BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, object, FetchBaseQueryMeta>,
        never,
        number[],                // Response type
        "api"                    // Your reducerPath
      >
    >;
    

    Change any to LazyGetTriggerType

    export const ChildComponent = ({ trigger }: { trigger: LazyGetTriggerType }) => {...}