Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong AsyncThunkAction args type when creating a thunk without args using create.asyncThunk #4060

Closed
DmitryScaletta opened this issue Jan 10, 2024 · 2 comments · Fixed by #4061 or #4389

Comments

@DmitryScaletta
Copy link
Contributor

Sometimes thunks doesn't require any args.
When creating a thunk without args using create.asyncThunk, the action still expects one argument.

Example

import { buildCreateSlice, asyncThunkCreator } from "@reduxjs/toolkit";

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const createSliceWithThunks = buildCreateSlice({
  creators: { asyncThunk: asyncThunkCreator },
});
export const counterSlice = createSliceWithThunks({
  name: "counter",
  initialState: { value: 0 },
  reducers: (create) => ({
    increment: create.asyncThunk(() => sleep(1000)),
    decrement: create.asyncThunk((_: undefined) => sleep(1000)),
  }),
});

export const { increment, decrement } = counterSlice.actions;

increment(); // ERROR: Expected 1 arguments, but got 0.
increment(undefined); // ok
decrement(); // ok

export default counterSlice.reducer;

Codesandbox: https://codesandbox.io/p/sandbox/quirky-sunset-8qsxf4?file=%2Fsrc%2FcounterSlice.ts%3A20%2C1

"@reduxjs/toolkit": "2.0.1",
"react-redux": "9.0.4"
@EskiMojo14
Copy link
Collaborator

EskiMojo14 commented Jan 10, 2024

Thanks for the report! Oddly it seems like I messed up when making the types for create.asyncThunk - they should be identical to createAsyncThunk's but aren't.

I've raised #4061 to fix this, but it's technically a breaking change so I'm waiting for confirmation whether it would be able to be in a less-than-major release.

In the meantime, that PR will have a build made by CodeSandbox that should fix the issue if installed.

edit: it'll be in the next release :)

@jeron-diovis
Copy link

@EskiMojo14 I'm experiencing same issue with version 2.2.3.

Very oddly, thunk type is inferred differently depending on how thunk is declared: as a part of reducers object, or separately.

import { asyncThunkCreator, buildCreateSlice } from "@reduxjs/toolkit";

const createAsyncSlice = buildCreateSlice({
  creators: {
    asyncThunk: asyncThunkCreator,
  },
});

type State = {};

const slice = createAsyncSlice({
  name: "demo",
  initialState: {} as State,

  reducers: (create) => {
    const standalone = create.asyncThunk(() => {});
    return {
      standalone,
      /* ^^^
        AsyncThunkSliceReducerDefinition<
          State, 
          void, // <--- ✅
          void, 
          PreventCircular<AsyncThunkConfig>
        >
      */

      inline: create.asyncThunk(() => {}),
      /* ^^^
        AsyncThunkSliceReducerDefinition<
          State, 
          any,  // <--- ❌
          void, 
          PreventCircular<AsyncThunkConfig>
        >
      */

      inline_explicit: create.asyncThunk<void>(() => {}),
    };
  },
});

const { standalone, inline, inline_explicit } = slice.actions;

standalone(); // ok
inline(); // Expected 1 arguments, but got 0.
inline_explicit(); // ok

Please see a reproduction playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants