From 36b4dd7f209c0fa1400f2017910168d416d36974 Mon Sep 17 00:00:00 2001 From: Daniel Lytkin Date: Tue, 24 Oct 2017 23:38:21 +0700 Subject: [PATCH] Add DeepPartial type for preloaded state (#2679) --- index.d.ts | 6 ++++-- test/typescript/store.ts | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3e2e960bbf..618336002a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -194,6 +194,8 @@ export interface Store { replaceReducer(nextReducer: Reducer): void; } +export type DeepPartial = { [K in keyof T]?: DeepPartial }; + /** * 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 { */ export interface StoreCreator { (reducer: Reducer, enhancer?: StoreEnhancer): Store; - (reducer: Reducer, preloadedState: S, enhancer?: StoreEnhancer): Store; + (reducer: Reducer, preloadedState: DeepPartial, enhancer?: StoreEnhancer): Store; } /** @@ -230,7 +232,7 @@ export interface StoreCreator { */ export type StoreEnhancer = (next: StoreEnhancerStoreCreator) => StoreEnhancerStoreCreator; export type GenericStoreEnhancer = StoreEnhancer; -export type StoreEnhancerStoreCreator = (reducer: Reducer, preloadedState?: S) => Store; +export type StoreEnhancerStoreCreator = (reducer: Reducer, preloadedState?: DeepPartial) => Store; /** * Creates a Redux store that holds the state tree. diff --git a/test/typescript/store.ts b/test/typescript/store.ts index b00b7d2cdc..067323e6ba 100644 --- a/test/typescript/store.ts +++ b/test/typescript/store.ts @@ -5,20 +5,29 @@ import { type State = { - todos: string[]; + a: 'a'; + b: { + c: 'c', + d: 'd', + }; } -const reducer: Reducer = (state: State, action: Action): State => { +const reducer: Reducer = (state: State | undefined = { + a: 'a', + b: { + c: 'c', + d: 'd', + }, +}, action: Action): State => { return state; -} - +}; /* createStore */ const store: Store = createStore(reducer); const storeWithPreloadedState: Store = createStore(reducer, { - todos: [] + b: {c: 'c'} }); const genericEnhancer: GenericStoreEnhancer = (next: StoreEnhancerStoreCreator) => next; @@ -28,7 +37,7 @@ const storeWithGenericEnhancer: Store = createStore(reducer, genericEnhan const storeWithSpecificEnhancer: Store = createStore(reducer, specificEnhancer); const storeWithPreloadedStateAndEnhancer: Store = createStore(reducer, { - todos: [] + b: {c: 'c'} }, genericEnhancer);