Skip to content

Commit 956e50d

Browse files
aikoventimdorr
authored andcommitted
Add DeepPartial type for preloaded state (#2679)
1 parent deb9ad1 commit 956e50d

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ export interface Store<S = any, A extends Action = Action, N = never> {
194194
replaceReducer(nextReducer: Reducer<S, A>): void;
195195
}
196196

197+
export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
198+
197199
/**
198200
* A store creator is a function that creates a Redux store. Like with
199201
* dispatching function, we must distinguish the base store creator,
@@ -206,7 +208,7 @@ export interface Store<S = any, A extends Action = Action, N = never> {
206208
*/
207209
export interface StoreCreator {
208210
<S, A extends Action, N>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
209-
<S, A extends Action, N>(reducer: Reducer<S, A>, preloadedState: S, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
211+
<S, A extends Action, N>(reducer: Reducer<S, A>, preloadedState: DeepPartial<S>, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
210212
}
211213

212214
/**
@@ -230,7 +232,7 @@ export interface StoreCreator {
230232
*/
231233
export type StoreEnhancer<N = never> = (next: StoreEnhancerStoreCreator<N>) => StoreEnhancerStoreCreator<N>;
232234
export type GenericStoreEnhancer<N = never> = StoreEnhancer<N>;
233-
export type StoreEnhancerStoreCreator<N = never> = <S = any, A extends Action = Action>(reducer: Reducer<S, A>, preloadedState?: S) => Store<S, A, N>;
235+
export type StoreEnhancerStoreCreator<N = never> = <S = any, A extends Action = Action>(reducer: Reducer<S, A>, preloadedState?: DeepPartial<S>) => Store<S, A, N>;
234236

235237
/**
236238
* Creates a Redux store that holds the state tree.

test/typescript/store.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,29 @@ import {
55

66

77
type State = {
8-
todos: string[];
8+
a: 'a';
9+
b: {
10+
c: 'c',
11+
d: 'd',
12+
};
913
}
1014

11-
const reducer: Reducer<State> = (state: State, action: Action): State => {
15+
const reducer: Reducer<State> = (state: State | undefined = {
16+
a: 'a',
17+
b: {
18+
c: 'c',
19+
d: 'd',
20+
},
21+
}, action: Action): State => {
1222
return state;
13-
}
14-
23+
};
1524

1625
/* createStore */
1726

1827
const store: Store<State> = createStore(reducer);
1928

2029
const storeWithPreloadedState: Store<State> = createStore(reducer, {
21-
todos: []
30+
b: {c: 'c'}
2231
});
2332

2433
const genericEnhancer: GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => next;
@@ -28,7 +37,7 @@ const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhan
2837
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhancer);
2938

3039
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(reducer, {
31-
todos: []
40+
b: {c: 'c'}
3241
}, genericEnhancer);
3342

3443

0 commit comments

Comments
 (0)