Permalink
Browse files

Add DeepPartial type for preloaded state (#2679)

  • Loading branch information...
aikoven authored and timdorr committed Oct 24, 2017
1 parent 92e8687 commit 037ea567e9808ef4ad281f3e19bb00c1e74587ba
Showing with 19 additions and 8 deletions.
  1. +4 −2 index.d.ts
  2. +15 −6 test/typescript/store.ts
View
@@ -194,6 +194,8 @@ export interface Store<S = any, A extends Action = Action, N = never> {
replaceReducer(nextReducer: Reducer<S, A>): void;
}
export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
/**
* A store creator is a function that creates a Redux store. Like with
* dispatching function, we must distinguish the base store creator,
@@ -206,7 +208,7 @@ export interface Store<S = any, A extends Action = Action, N = never> {
*/
export interface StoreCreator {
<S, A extends Action, N>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
<S, A extends Action, N>(reducer: Reducer<S, A>, preloadedState: S, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
<S, A extends Action, N>(reducer: Reducer<S, A>, preloadedState: DeepPartial<S>, enhancer?: StoreEnhancer<N>): Store<S, A, N>;
}
/**
@@ -230,7 +232,7 @@ export interface StoreCreator {
*/
export type StoreEnhancer<N = never> = (next: StoreEnhancerStoreCreator<N>) => StoreEnhancerStoreCreator<N>;
export type GenericStoreEnhancer<N = never> = StoreEnhancer<N>;
export type StoreEnhancerStoreCreator<N = never> = <S = any, A extends Action = Action>(reducer: Reducer<S, A>, preloadedState?: S) => Store<S, A, N>;
export type StoreEnhancerStoreCreator<N = never> = <S = any, A extends Action = Action>(reducer: Reducer<S, A>, preloadedState?: DeepPartial<S>) => Store<S, A, N>;
/**
* Creates a Redux store that holds the state tree.
View
@@ -5,20 +5,29 @@ import {
type State = {
todos: string[];
a: 'a';
b: {
c: 'c',
d: 'd',
};
}
const reducer: Reducer<State> = (state: State, action: Action): State => {
const reducer: Reducer<State> = (state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd',
},
}, action: Action): State => {
return state;
}
};
/* createStore */
const store: Store<State> = createStore(reducer);
const storeWithPreloadedState: Store<State> = createStore(reducer, {
todos: []
b: {c: 'c'}
});
const genericEnhancer: GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => next;
@@ -28,7 +37,7 @@ const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhan
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhancer);
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(reducer, {
todos: []
b: {c: 'c'}
}, genericEnhancer);

0 comments on commit 037ea56

Please sign in to comment.