22import { Action , AnyAction } from './actions'
33import { Reducer } from './reducers'
44
5+ /**
6+ * returns the most basic type that will not interfere with the existing type
7+ *
8+ * Note that for non-object stuff, this will mess with replaceReducer. The
9+ * assumption is that root reducers that only return a basic type (string, number, null, symbol) probably won't
10+ * be using replaceReducer anyways.
11+ */
12+ export type BaseType < S > = S extends { } ? { } : S
13+
514/**
615 * Internal "virtual" symbol used to make the `CombinedState` type unique.
716 */
@@ -107,8 +116,15 @@ export type Observer<T> = {
107116 *
108117 * @template S The type of state held by this store.
109118 * @template A the type of actions which may be dispatched by this store.
119+ * @template StateExt any extension to state from store enhancers
120+ * @template Ext any extensions to the store from store enhancers
110121 */
111- export interface Store < S = any , A extends Action = AnyAction > {
122+ export interface Store <
123+ S = any ,
124+ A extends Action = AnyAction ,
125+ StateExt = BaseType < S > ,
126+ Ext = { }
127+ > {
112128 /**
113129 * Dispatches an action. It is the only way to trigger a state change.
114130 *
@@ -179,9 +195,9 @@ export interface Store<S = any, A extends Action = AnyAction> {
179195 *
180196 * @param nextReducer The reducer for the store to use instead.
181197 */
182- replaceReducer < NewState = S , NewActions extends A = A > (
198+ replaceReducer < NewState , NewActions extends Action > (
183199 nextReducer : Reducer < NewState , NewActions >
184- ) : Store < NewState , NewActions >
200+ ) : Store < NewState & StateExt , NewActions , StateExt , Ext > & Ext
185201
186202 /**
187203 * Interoperability point for observable/reactive libraries.
@@ -204,15 +220,15 @@ export interface Store<S = any, A extends Action = AnyAction> {
204220 * @template StateExt State extension that is mixed into the state type.
205221 */
206222export interface StoreCreator {
207- < S , A extends Action , Ext , StateExt > (
223+ < S , A extends Action , Ext = { } , StateExt = BaseType < S > > (
208224 reducer : Reducer < S , A > ,
209225 enhancer ?: StoreEnhancer < Ext , StateExt >
210- ) : Store < S & StateExt , A > & Ext
211- < S , A extends Action , Ext , StateExt > (
226+ ) : Store < S & StateExt , A , StateExt , Ext > & Ext
227+ < S , A extends Action , Ext = { } , StateExt = BaseType < S > > (
212228 reducer : Reducer < S , A > ,
213229 preloadedState ?: PreloadedState < S > ,
214230 enhancer ?: StoreEnhancer < Ext >
215- ) : Store < S & StateExt , A > & Ext
231+ ) : Store < S & StateExt , A , StateExt , Ext > & Ext
216232}
217233
218234/**
@@ -237,12 +253,12 @@ export interface StoreCreator {
237253 * @template StateExt State extension that is mixed into the state type.
238254 */
239255export type StoreEnhancer < Ext = { } , StateExt = { } > = (
240- next : StoreEnhancerStoreCreator
256+ next : StoreEnhancerStoreCreator < Ext , StateExt >
241257) => StoreEnhancerStoreCreator < Ext , StateExt >
242258export type StoreEnhancerStoreCreator < Ext = { } , StateExt = { } > = <
243259 S = any ,
244260 A extends Action = AnyAction
245261> (
246262 reducer : Reducer < S , A > ,
247263 preloadedState ?: PreloadedState < S >
248- ) => Store < S & StateExt , A > & Ext
264+ ) => Store < S & StateExt , A , StateExt , Ext > & Ext
0 commit comments