Skip to content

Commit

Permalink
Only apply mapped types to un-branded types (reduxjs#3817)
Browse files Browse the repository at this point in the history
  • Loading branch information
invliD authored and Alok Kattangoori committed Sep 30, 2020
1 parent 2ee57c8 commit 54f9c5f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export type PreloadedState<S> = Required<S> extends {
}
: never
: {
[K in keyof S]: S[K] extends object ? PreloadedState<S[K]> : S[K]
[K in keyof S]: S[K] extends string | number | boolean | symbol
? S[K]
: PreloadedState<S[K]>
}

/**
Expand Down
35 changes: 25 additions & 10 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -24,7 +28,8 @@ const noExtend: ExtendState<State, never> = {
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
}
// typings:expect-error
const noExtendError: ExtendState<State, never> = {
Expand All @@ -33,7 +38,8 @@ const noExtendError: ExtendState<State, never> = {
c: 'c',
d: 'd'
},
e: 'oops'
e: brandedString,
f: 'oops'
}

const yesExtend: ExtendState<State, { yes: 'we can' }> = {
Expand All @@ -42,6 +48,7 @@ const yesExtend: ExtendState<State, { yes: 'we can' }> = {
c: 'c',
d: 'd'
},
e: brandedString,
yes: 'we can'
}
// typings:expect-error
Expand All @@ -50,7 +57,8 @@ const yesExtendError: ExtendState<State, { yes: 'we can' }> = {
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
}

interface DerivedAction extends Action {
Expand All @@ -64,7 +72,8 @@ const reducer: Reducer<State> = (
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
},
action: Action
): State => {
Expand All @@ -77,7 +86,8 @@ const reducerWithAction: Reducer<State, DerivedAction> = (
b: {
c: 'c',
d: 'd'
}
},
e: brandedString
},
action: DerivedAction
): State => {
Expand All @@ -95,17 +105,20 @@ const arrayReducer = (state: any[] = []) => state || []
const storeWithArrayState: Store<any[]> = createStore(arrayReducer)
const storeWithPreloadedState: Store<State> = createStore(reducer, {
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
e: brandedString
})
// typings:expect-error
const storeWithBadPreloadedState: Store<State> = 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)
Expand All @@ -114,7 +127,8 @@ funcWithStore(storeWithActionReducerAndPreloadedState)
const storeWithActionReducerAndBadPreloadedState = createStore(
reducerWithAction,
{
b: { c: 'c' }
b: { c: 'c' },
e: brandedString
}
)

Expand All @@ -126,7 +140,8 @@ const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
a: 'a',
b: { c: 'c', d: 'd' }
b: { c: 'c', d: 'd' },
e: brandedString
},
enhancer
)
Expand Down

0 comments on commit 54f9c5f

Please sign in to comment.