Recently, while working on a React app that utilizes react-redux and react-toolkit, I encountered some TypeScript syntax that left me puzzled.
export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch
export const useAppDispatch: () => AppDispatch = useDispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
One particular line that confused me was the usage of '=':
export const useAppDispatch: () => AppDispatch = useDispatch
Initially, I thought it was simply casting the type, similar to userInput as string
. However, when I attempted to modify the code to
export const useAppDispatch: () => useDispatch as AppDispatch
, my IDE started throwing numerous errors.
Could someone explain the purpose of this specific line of code?