-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
I think it's clear that Redux Thunk doesn't provide the best experience for TypeScript developers. A Google search for “redux thunk typescript” yields many results with conflicting and outdated information with different ways to do the same thing. As a TypeScript developer, trying to enter this space can become frustrating very quickly. This is why I'd like to propose some changes we can make to improve this situation.
Breaking the single-letter naming convention for generic types
The most used naming convention for generics in the TypeScript community is using single-letters for type names. It's also what's used in the official TypeScript documentation. While this can work well for simple components that don't require much explanation, it can become quite tedious to work with as the component you're working with becomes more complex.
Take this type, for example.
export type ThunkAction<R, S, E, A extends Action> = (
dispatch: ThunkDispatch<S, E, A>,
getState: () => S,
extraArgument: E
) => R;
Without actually looking at the inside code of the type, the only variable type I could guess was S
(which is the store state). Only when you start looking at the code, it becomes apparent that R
is the result type by looking at which type the function returns, A
is an Action
by looking at what it's extending and that E
is the extra argument by looking at its property name. This also means that if you're using an IDE, the parameter (or generic) info become virtually useless as you'll have to look at the definition anyway because it only works with descriptive names.
This is why I think we should consider using one of these naming conventions:
ThunkAction<ResultType, StateType, ExtraArgumentType, ActionType extends Action>
or
ThunkAction<TResult, TState, TExtraArgument, TAction extends Action>
I personally prefer the second one, as I've seen other codebases use it before and it also prevents you from having to write ActionType
, which could conflict with another type you might have in a codebase using Redux.
The main downside of changing the naming convention is that we would be breaking the convention that's used in most TypeScript codebases as well as in official documentation. Ironically, I think it would improve readability.
Writing documentation on how to use the types with real world examples
There are 3 components in the current declaration file: ThunkDispatch
, ThunkAction
and ThunkMiddleware
. While these names are fairly descriptive and self-explanatory, I think new Redux-TypeScript users would benefit greatly from official documentation with some good examples. At times, it can be difficult to use all the types correctly in conjunction with each other due to the way Redux is designed. This documentation could fit well into a separate TypeScript section on the official Redux documentation webpage, or we could keep it simple without going too much in depth and just put comments into the declarations file. The problem is that people might not always look at the declarations file for documentation.
I'm interested to see what everyone thinks of these two proposals, and what else we could do to help the TypeScript community.