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 type for the result of dispatching a createAsyncThunk

See original GitHub issue

Thanks for an awesome library!

I asked on SO but wasn’t able to get a definitive answer around this. I’ve also seen a few similar issues here but I’m using the connect API instead of the useDispatch hook which most of those examples use. Given the code below, what should the type be for my loadAction so that TS knows this is a promise and that it has a type property.

interface ApiResponse {
  data: string;
}

interface ApiErrorResponse {
  message: string;
}

const api = () =>
  new Promise<ApiResponse>((resolve) => {
    resolve({ data: "123" });
  });

const loadSomething = createAsyncThunk<
  string,
  void,
  { rejectValue: ApiErrorResponse }
>("something", async (_, { rejectWithValue }) => {
  try {
    const result = await api();
    return result.data;
  } catch (err) {
    if (!err.response) throw err;

    return rejectWithValue(err.data as ApiErrorResponse);
  }
});

const List = ({ data = "Something", loadAction }: ListProps) => {
  React.useEffect(() => {
    const fetchData = async () => {
      const result = await loadAction();
      if (result.type.endsWith("rejected")) alert("Failed");
    };
    fetchData();
  }, [loadAction]);

  return <div>#{data}</div>;
};

interface DispatchProps {
  loadAction: () => AsyncThunkAction<string, void, {}>; //This doesn't work so what type should this be?
}

interface OwnProps {
  data?: string;
}

type ListProps = DispatchProps & OwnProps;

const mapDispatch = {
  loadAction: loadSomething
};

const ListContainer = connect<void, DispatchProps, OwnProps, {}>(
  null,
  mapDispatch
)(List);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Dec 18, 2020

or, y’know, ({asyncThunk}: PropsFromRedux) => { 😃

0reactions
hoffmanilyacommented, Dec 22, 2020

@msutkowski thanks for the advice! I’ll give the useDispatch route a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use createAsyncThunk with Typescript? How to set ...
I'm assuming the return of the createAsyncThunk async handler is the payload for the fulfilled action, is that right? But how can I...
Read more >
Usage With TypeScript - Redux Toolkit
Details on how to use each Redux Toolkit API with TypeScript ... If you want to get the Dispatch type from your store,...
Read more >
How to Use Thunks with Redux Toolkit and TypeScript
`createAsyncThunk` is a generic function. // We can use the first type-parameter // to tell what type will be returned as a result....
Read more >
Accessing Global State inside of Async Thunks with TypeScript
The payloadCreator argument to createAsyncThunk takes two arguments. ... data passed into the thunk's action creator such as dispatch(checkout(items)).
Read more >
createAsyncThunk in Redux-Toolkit | by Abhimanyu Chauhan
Dispatching async actions never looked so good ... According to the official docs: createAsyncThunk is a function that accepts a Redux action type...
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