Next.js Discord

Discord Forum

Redux toolkit and typescript payload come from createAsyncThunk is unknown

Answered
Snowshoe posted this in #help-forum
Open in Discord
SnowshoeOP
Answered by Ray
try throw error instead of return error
View full answer

24 Replies

SnowshoeOP
My full code
when I give type of the action on addCase asyncThunk.fulfilled
do you have this type exported?
// store.ts

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch

//hooks.ts

import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import type { RootState, AppDispatch } from './store'

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch = () => useDispatch<AppDispatch>()
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
SnowshoeOP
in store.ts:
import { configureStore } from '@reduxjs/toolkit' import { linkchiselSlice } from './features/linkchisel/linkchiselSlice' export const store = () => { return configureStore({ reducer: { linkchisel: linkchiselSlice } }) } export type AppStore = ReturnType<typeof store> export type RootState = ReturnType<AppStore['getState']> export type AppDispatch = AppStore['dispatch']

in hooks.ts:
import { useDispatch, useSelector, useStore } from 'react-redux' import type { TypedUseSelectorHook } from 'react-redux' import type { RootState, AppDispatch, AppStore } from './store' export const useAppDispatch: () => AppDispatch = useDispatch export const useAppStore: () => AppStore = useStore const useAppSelector: TypedUseSelectorHook<RootState> = useSelector export default useAppSelector((state) => state.linkchisel)
SnowshoeOP
nothing happened
what is asyncThunk there?
ah i see it
does it return anything?
SnowshoeOP
it is return unknown in action.payload instead of data
the screenshot 1, I cannot see if the function return any thing
@Ray the screenshot 1, I cannot see if the function return any thing
SnowshoeOP
I want to return data that have linkchiselType instead of unkown
can i see the whole function
do you return anything in the catch block
SnowshoeOP
there are screenshots of all page in the first message
@Snowshoe My full code
SnowshoeOP
.
ah found it
try throw error instead of return error
Answer
the error is uknown type
SnowshoeOP
yes thank you so much I never noticed this :heartvercel:
basically, if you are not doing extra error handling, I think you do not need to use try/catch there