TypeScript type for the result of dispatching a createAsyncThunk
See original GitHub issueThanks 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
or, y’know,
({asyncThunk}: PropsFromRedux) => {
😃@msutkowski thanks for the advice! I’ll give the
useDispatch
route a try.