From b41174f663aec4aa4e9319d5497a20b994346062 Mon Sep 17 00:00:00 2001 From: Chris Ackerman Date: Mon, 12 Aug 2019 08:24:26 -0700 Subject: [PATCH] #2808 Preloaded state is now selectively partial (instead of deeply partial). (#3485) * Preloaded state is now selectively partial (instead of deeply partial). * Improved CombinedState, PreloadedState, and removed UnCombinedState. Found a better way to type check CombinedState which allows the $CombinedState symbol property marker to be optional. Since it's optional, it's no longer necessary to strip it off in the Reducer state parameter type and return type. This leaves the type definition for Reducer unmodified, reduces the number of types required by one, and makes the resolved types and stack traces clearer. * Small change to the description of CombinedState. * Removed DeepPartial import from tests. Leaving the definition in place as removing it would be a breaking change. * Made prettier happy. * Made prettier happy with UsingObjectSpreadOperator.md Former-commit-id: 41c0612dbf646562ccbdf10117930690a0587172 --- docs/recipes/UsingObjectSpreadOperator.md | 3 ++- index.d.ts.REMOVED.git-id | 2 +- test/typescript/enhancers.ts | 14 ++++-------- test/typescript/store.ts | 26 ++++++++++++++++++++++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/docs/recipes/UsingObjectSpreadOperator.md b/docs/recipes/UsingObjectSpreadOperator.md index cb129f33158..fa860b867a2 100644 --- a/docs/recipes/UsingObjectSpreadOperator.md +++ b/docs/recipes/UsingObjectSpreadOperator.md @@ -64,6 +64,7 @@ While the object spread syntax is a [Stage 4](https://github.com/tc39/proposal-o "plugins": ["@babel/plugin-proposal-object-rest-spread"] } ``` + > ##### Note on Object Spread Operator -> Like the Array Spread Operator, the Object Spread Operator creates a [shallow clone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) of the original object. In other words, for multidimensional source objects, elements in the copied object at a depth greater than one are mere references to the source object (with the exception of [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), which are copied). Thus, you cannot reliably use the Object Spread Operator (`...`) for deep cloning objects. +> Like the Array Spread Operator, the Object Spread Operator creates a [shallow clone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) of the original object. In other words, for multidimensional source objects, elements in the copied object at a depth greater than one are mere references to the source object (with the exception of [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), which are copied). Thus, you cannot reliably use the Object Spread Operator (`...`) for deep cloning objects. diff --git a/index.d.ts.REMOVED.git-id b/index.d.ts.REMOVED.git-id index 556c8371904..34be350d2c4 100644 --- a/index.d.ts.REMOVED.git-id +++ b/index.d.ts.REMOVED.git-id @@ -1 +1 @@ -c9c5a57d757e5b58e699c9b8ec15a14e5843521f \ No newline at end of file +50e8d018faee8887ed82337fcd1993ad07df0968 \ No newline at end of file diff --git a/test/typescript/enhancers.ts b/test/typescript/enhancers.ts index 4670f29f78a..7259b4884bf 100644 --- a/test/typescript/enhancers.ts +++ b/test/typescript/enhancers.ts @@ -1,11 +1,5 @@ -import { - StoreEnhancer, - Action, - AnyAction, - Reducer, - createStore, - DeepPartial -} from 'redux' +import { PreloadedState } from '../../index' +import { StoreEnhancer, Action, AnyAction, Reducer, createStore } from 'redux' interface State { someField: 'string' @@ -43,10 +37,10 @@ function stateExtension() { A extends Action = AnyAction >( reducer: Reducer, - preloadedState?: DeepPartial + preloadedState?: PreloadedState ) => { const wrappedReducer: Reducer = null as any - const wrappedPreloadedState: S & ExtraState = null as any + const wrappedPreloadedState: PreloadedState = null as any return createStore(wrappedReducer, wrappedPreloadedState) } diff --git a/test/typescript/store.ts b/test/typescript/store.ts index fb1974ab41b..123464ce556 100644 --- a/test/typescript/store.ts +++ b/test/typescript/store.ts @@ -57,21 +57,45 @@ const funcWithStore = (store: Store) => {} const store: Store = createStore(reducer) const storeWithPreloadedState: Store = createStore(reducer, { + a: 'a', + b: { c: 'c', d: 'd' } +}) +// typings:expect-error +const storeWithBadPreloadedState: Store = createStore(reducer, { b: { c: 'c' } }) const storeWithActionReducer = createStore(reducerWithAction) const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, { - b: { c: 'c' } + a: 'a', + b: { c: 'c', d: 'd' } }) funcWithStore(storeWithActionReducer) funcWithStore(storeWithActionReducerAndPreloadedState) +// typings:expect-error +const storeWithActionReducerAndBadPreloadedState = createStore( + reducerWithAction, + { + b: { c: 'c' } + } +) + const enhancer: StoreEnhancer = next => next const storeWithSpecificEnhancer: Store = createStore(reducer, enhancer) const storeWithPreloadedStateAndEnhancer: Store = createStore( + reducer, + { + a: 'a', + b: { c: 'c', d: 'd' } + }, + enhancer +) + +// typings:expect-error +const storeWithBadPreloadedStateAndEnhancer: Store = createStore( reducer, { b: { c: 'c' }