reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.71k stars 1.17k forks source link

type issue with thunk action return type using condition #674

Closed ShravanSunder closed 4 years ago

ShravanSunder commented 4 years ago

I have the following thunk action

const getStore = createAsyncThunk(
    `timeline/store/getStore`,
    async ({ storeGuid }: { storeGuid: string }, thunkApi): any => {
        return await getStoreApi(storeGuid);
    },
    {
        condition: ({storeGuid}: {storeGuid: string}, thunkApi) : boolean | undefined=> {
            let s = storeInfoSelector(thunkapi.getState());
            if (s.storeGuid == storeGuid || storeGuid == null) return false;
        },
    }
)
When i don't have the condition  parameter the getStore type is
const getStore: ((arg: {
    storeGuid: string;
}) => AsyncThunkAction<any, {
    storeGuid: string;
}, {}>) & {
    pending: ActionCreatorWithPreparedPayload<[string, {
        storeGuid: string;
    }], undefined, string, never, {
        arg: {
            storeGuid: string;
        };
        requestId: string;
    }>;

however when i add the condition the type changes to


const getStore: (() => AsyncThunkAction<unknown, void, {}>) & {
    pending: ActionCreatorWithPreparedPayload<[string, void], undefined, string, never, {
        arg: void;
        requestId: string;
    }>;
    rejected: ActionCreatorWithPreparedPayload<...>;
    fulfilled: ActionCreatorWithPreparedPayload<...>;
    typePrefix: string;
}

The issue is with the <unknown, void, {}> versus <any, {storeGuid: string;}, {}> i can't currently use the thunk properly in my component as typescript complains about the function used with a parameter when none is expected.

I'm not sure how to force the thunk type to retain my typing

> TS2554: Expected 0 arguments, but got 1.

phryneas commented 4 years ago

I just tested this in a codesandbox (see https://codesandbox.io/s/eloquent-keller-vp85m?file=/src/App.tsx ) and it seems to be working correct for me. Can you provide a reproduction of this?

Also, in general once you start using thunkApi.getState() usually you need to provide generic arguments as described in the documentation: https://redux-toolkit.js.org/usage/usage-with-typescript#createasyncthunk

phryneas commented 4 years ago

Also, are you using WebStorm? We've had another case where in WebStorm, incorrect types were shown because they try to speed up things with an incorrect algorithm.

ShravanSunder commented 4 years ago

i'm not using webstorm. Its a react app hosted in asp.NET. part of the app is in plain js. And some of it is in typescript.
I"ve been converting it to typescript as i go.

I tried recreating this in codesandbox and it seems to work fine. i'm trying to think of how i could send a reproduction other wise

ShravanSunder commented 4 years ago

oh once i added the generic arguments it worked!

const getStore = createAsyncThunk<any, getStoreType, any>(

thanks so much for the pointer! @phryneas