From 54f9c5f73c3b8c7e4ab27a44176b09cbdd9105a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Br=C3=BCckner?= Date: Thu, 16 Jul 2020 16:15:47 +0200 Subject: [PATCH] Only apply mapped types to un-branded types (#3817) --- src/types/store.ts | 4 +++- test/typescript/store.ts | 35 +++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/types/store.ts b/src/types/store.ts index c74d5f8908..7bfda3ac8b 100644 --- a/src/types/store.ts +++ b/src/types/store.ts @@ -53,7 +53,9 @@ export type PreloadedState = Required extends { } : never : { - [K in keyof S]: S[K] extends object ? PreloadedState : S[K] + [K in keyof S]: S[K] extends string | number | boolean | symbol + ? S[K] + : PreloadedState } /** diff --git a/test/typescript/store.ts b/test/typescript/store.ts index dd7d76296e..5817388583 100644 --- a/test/typescript/store.ts +++ b/test/typescript/store.ts @@ -10,12 +10,16 @@ import { } from '../..' import 'symbol-observable' +type BrandedString = string & { _brand: 'type' } +const brandedString = 'a string' as BrandedString + type State = { a: 'a' b: { c: 'c' d: 'd' } + e: BrandedString } /* extended state */ @@ -24,7 +28,8 @@ const noExtend: ExtendState = { b: { c: 'c', d: 'd' - } + }, + e: brandedString } // typings:expect-error const noExtendError: ExtendState = { @@ -33,7 +38,8 @@ const noExtendError: ExtendState = { c: 'c', d: 'd' }, - e: 'oops' + e: brandedString, + f: 'oops' } const yesExtend: ExtendState = { @@ -42,6 +48,7 @@ const yesExtend: ExtendState = { c: 'c', d: 'd' }, + e: brandedString, yes: 'we can' } // typings:expect-error @@ -50,7 +57,8 @@ const yesExtendError: ExtendState = { b: { c: 'c', d: 'd' - } + }, + e: brandedString } interface DerivedAction extends Action { @@ -64,7 +72,8 @@ const reducer: Reducer = ( b: { c: 'c', d: 'd' - } + }, + e: brandedString }, action: Action ): State => { @@ -77,7 +86,8 @@ const reducerWithAction: Reducer = ( b: { c: 'c', d: 'd' - } + }, + e: brandedString }, action: DerivedAction ): State => { @@ -95,17 +105,20 @@ const arrayReducer = (state: any[] = []) => state || [] const storeWithArrayState: Store = createStore(arrayReducer) const storeWithPreloadedState: Store = createStore(reducer, { a: 'a', - b: { c: 'c', d: 'd' } + b: { c: 'c', d: 'd' }, + e: brandedString }) // typings:expect-error const storeWithBadPreloadedState: Store = createStore(reducer, { - b: { c: 'c' } + b: { c: 'c' }, + e: brandedString }) const storeWithActionReducer = createStore(reducerWithAction) const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, { a: 'a', - b: { c: 'c', d: 'd' } + b: { c: 'c', d: 'd' }, + e: brandedString }) funcWithStore(storeWithActionReducer) funcWithStore(storeWithActionReducerAndPreloadedState) @@ -114,7 +127,8 @@ funcWithStore(storeWithActionReducerAndPreloadedState) const storeWithActionReducerAndBadPreloadedState = createStore( reducerWithAction, { - b: { c: 'c' } + b: { c: 'c' }, + e: brandedString } ) @@ -126,7 +140,8 @@ const storeWithPreloadedStateAndEnhancer: Store = createStore( reducer, { a: 'a', - b: { c: 'c', d: 'd' } + b: { c: 'c', d: 'd' }, + e: brandedString }, enhancer )