Skip to content

TypeScript: Add DeepPartial type for preloaded state #2679

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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