-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
fix replaceReducer with a store enhancer #3524
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
Changes from all commits
2fcea31
cacaecd
115e78c
5b2957a
7d8acc2
1d0d11a
554b79e
4713921
7168df6
66b6090
6e60f2d
2a5acb6
3f8cfb5
806f2aa
c736cf3
bbfb018
f5f796c
b95330c
b3ae3d5
4270e0e
4ed78cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,15 +247,33 @@ export type Observer<T> = { | |
next?(value: T): void | ||
} | ||
|
||
/** | ||
* Extend the state | ||
* | ||
* This is used by store enhancers and store creators to extend state. | ||
* If there is no state extension, it just returns the state, as is, otherwise | ||
* it returns the state joined with its extension. | ||
*/ | ||
export type ExtendState<State, Extension> = [Extension] extends [never] | ||
? State | ||
: State & Extension | ||
|
||
/** | ||
* A store is an object that holds the application's state tree. | ||
* There should only be a single store in a Redux app, as the composition | ||
* happens on the reducer level. | ||
* | ||
* @template S The type of state held by this store. | ||
* @template A the type of actions which may be dispatched by this store. | ||
* @template StateExt any extension to state from store enhancers | ||
* @template Ext any extensions to the store from store enhancers | ||
*/ | ||
export interface Store<S = any, A extends Action = AnyAction> { | ||
export interface Store< | ||
S = any, | ||
A extends Action = AnyAction, | ||
StateExt = never, | ||
Ext = {} | ||
Comment on lines
+274
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, would it be possible to make |
||
> { | ||
/** | ||
* Dispatches an action. It is the only way to trigger a state change. | ||
* | ||
|
@@ -326,9 +344,9 @@ export interface Store<S = any, A extends Action = AnyAction> { | |
* | ||
* @param nextReducer The reducer for the store to use instead. | ||
*/ | ||
replaceReducer<NewState = S, NewActions extends A = A>( | ||
replaceReducer<NewState, NewActions extends Action>( | ||
nextReducer: Reducer<NewState, NewActions> | ||
): Store<NewState, NewActions> | ||
): Store<ExtendState<NewState, StateExt>, NewActions, StateExt, Ext> & Ext | ||
|
||
/** | ||
* Interoperability point for observable/reactive libraries. | ||
|
@@ -355,15 +373,15 @@ export type DeepPartial<T> = { | |
* @template StateExt State extension that is mixed into the state type. | ||
*/ | ||
export interface StoreCreator { | ||
<S, A extends Action, Ext, StateExt>( | ||
<S, A extends Action, Ext = {}, StateExt = never>( | ||
reducer: Reducer<S, A>, | ||
enhancer?: StoreEnhancer<Ext, StateExt> | ||
): Store<S & StateExt, A> & Ext | ||
<S, A extends Action, Ext, StateExt>( | ||
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext | ||
<S, A extends Action, Ext = {}, StateExt = never>( | ||
reducer: Reducer<S, A>, | ||
preloadedState?: PreloadedState<S>, | ||
enhancer?: StoreEnhancer<Ext> | ||
): Store<S & StateExt, A> & Ext | ||
): Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext | ||
} | ||
|
||
/** | ||
|
@@ -417,16 +435,17 @@ export const createStore: StoreCreator | |
* @template Ext Store extension that is mixed into the Store type. | ||
* @template StateExt State extension that is mixed into the state type. | ||
*/ | ||
export type StoreEnhancer<Ext = {}, StateExt = {}> = ( | ||
next: StoreEnhancerStoreCreator | ||
export type StoreEnhancer<Ext = {}, StateExt = never> = ( | ||
next: StoreEnhancerStoreCreator<Ext, StateExt> | ||
) => StoreEnhancerStoreCreator<Ext, StateExt> | ||
export type StoreEnhancerStoreCreator<Ext = {}, StateExt = {}> = < | ||
|
||
export type StoreEnhancerStoreCreator<Ext = {}, StateExt = never> = < | ||
S = any, | ||
A extends Action = AnyAction | ||
>( | ||
reducer: Reducer<S, A>, | ||
preloadedState?: PreloadedState<S> | ||
) => Store<S & StateExt, A> & Ext | ||
) => Store<ExtendState<S, StateExt>, A, StateExt, Ext> & Ext | ||
|
||
/* middleware */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ export default function applyMiddleware<Ext, S = any>( | |
): StoreEnhancer<{ dispatch: Ext }> | ||
export default function applyMiddleware( | ||
...middlewares: Middleware[] | ||
): StoreEnhancer { | ||
): StoreEnhancer<any> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fix ends up being fine because the overloads provide more restrictive typing (you can check in |
||
return (createStore: StoreCreator) => <S, A extends AnyAction>( | ||
reducer: Reducer<S, A>, | ||
...args: any[] | ||
|
Uh oh!
There was an error while loading. Please reload this page.