New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TypedUseDispatchHook interface for typing and using with middlewares like redux thunk #1650
Comments
We already cover how to handle typing https://react-redux.js.org/using-react-redux/static-typing#typing-the-usedispatch-hook https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type Although looking at those two pages, the Redux Toolkit page talks about I'd appreciate a PR that updates the React-Redux page with the Having said that, it would seem like |
@markerikson I'd gladly open a PR to update React-Redux page with Also I think something like what I'd say before could be useful specially for apps migrating from JS to TS. For example: import { Action } from 'redux';
import {
useDispatch as useReactReduxDispatch,
useSelector as useReactReduxSelector,
TypedUseSelectorHook,
} from 'react-redux';
import { ThunkDispatch } from 'redux-thunk';
import { AppState } from 'path/to/app/root/state';
export type TypedDispatch = ThunkDispatch<AppState, unknown, Action>;
export interface TypedUseDispatchHook<TState> {
(): TState;
}
export const useSelector: TypedUseSelectorHook<AppState> = useReactReduxSelector;
export const useDispatch: TypedUseDispatchHook<TypedDispatch> = useReactReduxDispatch; So after using these interfaces, there is no need to change any other part of codebase. |
Still not sure what you're asking us to actually add beyond what that Copying @phryneas , our resident TS expert. |
I'd say what you are describing there pretty much matches what we already have in the docs that Mark linked to above. |
@IVIosi |
@phryneas @markerikson @dai-shi Thanks for your comments. I found out there are different approaches for static typing. So no need for helper type I mentioned in the beginning. |
Do you want to request a feature or report a bug?
Feature
What is the expected behavior?
add an interface like TypedUseSelectorHook for useDispatch hook
Details
I think an interface for typing
useDispatch
hook like whatuseSelector
hook has could be useful for many users who uses redux middlewares for async actions (e.g. redux thunk and etc.)The text was updated successfully, but these errors were encountered: