question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript createAsyncThunk optional or no args

See original GitHub issue

I am trying to make an api call with an optional parameter however typescript complains

export const usersLoad = createAsyncThunk(
	'users/load',
	(filters: UserFilters = {}) => Api.get<User[]>('/api/users/search', filters)
);
...
dispatch(usersLoad());

Expected 1 arguments, but got 0

I have a similar issue with this action

export const usersLoadMore = createAsyncThunk(
	'users/loadMore',
	(_: SyntheticEvent | void, thunkApi) => {
		const state = thunkApi.getState() as RootState;
		const { filters, data } = state.users;
		return Api.get<User[]>('/api/users/search', {
			...filters,
			offset: data.length,
		});
	}
);

here I don’t use the arg but I had to give it the SyntheticEvent or it complained when passing it to onClick, and I had to give it void to make it work when I call it directly. I would think that I would not have to set it to anything since I am not using it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:27 (7 by maintainers)

github_iconTop GitHub Comments

12reactions
phryneascommented, Apr 10, 2020

Yes. As I said:

that’s not possible at the moment. I will look into this in the near future

We simply didn’t anticipate this use case and none of our beta testers had it either, so we’ll have to add it.

7reactions
BeigeBadgercommented, Jul 14, 2021

For anyone who comes across this in the future, I solved the no argument requirement in my TypeScript application by using the _ discard operator as the first parameter to the payloadCreator function - whether this is correct or not is another matter.

An example is below:

export const getAllQuizzes = createAsyncThunk<QuizItem[]>(
    'quizzes/fetchAll',
    async (_, thunkAPI) => {
        thunkAPI.dispatch(pageStackLoading());

        const response = await fetch(`/api/quiz/all`);
        const jsonPayload = await response.json();

        thunkAPI.dispatch(pageStackReceived());

        // Will dispatch the `fulfilled` action
        return jsonPayload;
    }
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use createAsyncThunk from Redux Toolkit with ...
Expected 1 arguments, but got 0. createAsyncThunk.d.ts(107, 118): An argument for 'arg' was not provided. I have similar code in the project ...
Read more >
createAsyncThunk() function do I need to pass a parameter in ...
Hello! While using reduxjs/toolkit - createAsyncThunk() function do I need to pass a parameter in the async() function?
Read more >
createAsyncThunk - Redux Toolkit
An object with the following optional fields: condition(arg, { getState, extra } ): boolean | Promise<boolean> : a callback that can be used...
Read more >
Using Redux Toolkit's createAsyncThunk - LogRocket Blog
While a Redux store possesses great state management features, it has no clue how to deal with asynchronous logic.
Read more >
How to Use Thunks with Redux Toolkit and TypeScript
import { createAsyncThunk } from "@reduxjs/toolkit"; ​ export const fetchTodos = createAsyncThunk( // The first argument is the action name: "todos/fetch",
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found