Skip to content

Commit

Permalink
Add DeepPartial type for preloaded state (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
aikoven authored and timdorr committed Nov 3, 2017
1 parent deb9ad1 commit 956e50d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Expand Up @@ -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,
Expand All @@ -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>;
}

/**
Expand All @@ -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.
Expand Down
21 changes: 15 additions & 6 deletions test/typescript/store.ts
Expand Up @@ -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;
Expand All @@ -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);


Expand Down

0 comments on commit 956e50d

Please sign in to comment.