-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Update TypeScript typings #2773
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
Conversation
| * @template A The type of actions the reducer can potentially respond to. | ||
| */ | ||
| export type Reducer<S = any, A extends Action = Action> = (state: S | undefined, action: A) => S; | ||
| export type Reducer<S = any, A extends Action = AnyAction> = (state: S | undefined, action: A) => S; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this is not needed, because any interface with additional properties extends Action interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default type for A being AnyAction is needed so that any reducer acting on the state State would be assignable to Reducer<State> (with omitted action type parameter).
You may want to change it back to Action and check out the tests that break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see now. Thanks.
|
Since no one has come in and yelled "Holy balls, this is terrible!", I'm going to assume this is at least somewhat of an improvement and merge it in. I swear, I'll learn TS at some point... |
|
Edited because it unintentionally could have been read as hostile. Not intended! 🙂 Hey folks 👋 I'm curious if someone could elaborate on the decision to change the order of the generic params |
|
@jayphelps Old export interface MiddlewareAPI<S> {
dispatch: Dispatch<S>;
getState(): S;
}The new one has a default for it: export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
dispatch: D;
getState(): S;
}If you don't need a custom dispatch signature, you can just use the built-in
That's why we have bumped a major version 🙂 |
This PR continues work from #2563 and fixes #1648, #2602 and #2740.
Tests are rewritten to cover different supported approaches. All TS strict checks are enabled in tests.
Reducers
combineReducersare updated to allow strict combination using discriminated unions, as well as combining custom and third-party reducers.See tests.
Dispatch
Dispatchtype only accepts plain actions. It also supports restricting action types using discriminated unions.Dispatch & PromiseDispatch.See tests and tests for injected dispatch e.g.
connectfromreact-redux.Middleware
applyMiddlewareenhancer.See tests.
Store Enhancers
See tests.
cc. @pelotom @KingHenne