Skip to content

Commit 0dae2d1

Browse files
authored
Only apply mapped types to un-branded types (#3817)
1 parent b08cf38 commit 0dae2d1

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/types/store.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export type PreloadedState<S> = Required<S> extends {
5353
}
5454
: never
5555
: {
56-
[K in keyof S]: S[K] extends object ? PreloadedState<S[K]> : S[K]
56+
[K in keyof S]: S[K] extends string | number | boolean | symbol
57+
? S[K]
58+
: PreloadedState<S[K]>
5759
}
5860

5961
/**

test/typescript/store.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import {
1010
} from '../..'
1111
import 'symbol-observable'
1212

13+
type BrandedString = string & { _brand: 'type' }
14+
const brandedString = 'a string' as BrandedString
15+
1316
type State = {
1417
a: 'a'
1518
b: {
1619
c: 'c'
1720
d: 'd'
1821
}
22+
e: BrandedString
1923
}
2024

2125
/* extended state */
@@ -24,7 +28,8 @@ const noExtend: ExtendState<State, never> = {
2428
b: {
2529
c: 'c',
2630
d: 'd'
27-
}
31+
},
32+
e: brandedString
2833
}
2934
// typings:expect-error
3035
const noExtendError: ExtendState<State, never> = {
@@ -33,7 +38,8 @@ const noExtendError: ExtendState<State, never> = {
3338
c: 'c',
3439
d: 'd'
3540
},
36-
e: 'oops'
41+
e: brandedString,
42+
f: 'oops'
3743
}
3844

3945
const yesExtend: ExtendState<State, { yes: 'we can' }> = {
@@ -42,6 +48,7 @@ const yesExtend: ExtendState<State, { yes: 'we can' }> = {
4248
c: 'c',
4349
d: 'd'
4450
},
51+
e: brandedString,
4552
yes: 'we can'
4653
}
4754
// typings:expect-error
@@ -50,7 +57,8 @@ const yesExtendError: ExtendState<State, { yes: 'we can' }> = {
5057
b: {
5158
c: 'c',
5259
d: 'd'
53-
}
60+
},
61+
e: brandedString
5462
}
5563

5664
interface DerivedAction extends Action {
@@ -64,7 +72,8 @@ const reducer: Reducer<State> = (
6472
b: {
6573
c: 'c',
6674
d: 'd'
67-
}
75+
},
76+
e: brandedString
6877
},
6978
action: Action
7079
): State => {
@@ -77,7 +86,8 @@ const reducerWithAction: Reducer<State, DerivedAction> = (
7786
b: {
7887
c: 'c',
7988
d: 'd'
80-
}
89+
},
90+
e: brandedString
8191
},
8292
action: DerivedAction
8393
): State => {
@@ -95,17 +105,20 @@ const arrayReducer = (state: any[] = []) => state || []
95105
const storeWithArrayState: Store<any[]> = createStore(arrayReducer)
96106
const storeWithPreloadedState: Store<State> = createStore(reducer, {
97107
a: 'a',
98-
b: { c: 'c', d: 'd' }
108+
b: { c: 'c', d: 'd' },
109+
e: brandedString
99110
})
100111
// typings:expect-error
101112
const storeWithBadPreloadedState: Store<State> = createStore(reducer, {
102-
b: { c: 'c' }
113+
b: { c: 'c' },
114+
e: brandedString
103115
})
104116

105117
const storeWithActionReducer = createStore(reducerWithAction)
106118
const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, {
107119
a: 'a',
108-
b: { c: 'c', d: 'd' }
120+
b: { c: 'c', d: 'd' },
121+
e: brandedString
109122
})
110123
funcWithStore(storeWithActionReducer)
111124
funcWithStore(storeWithActionReducerAndPreloadedState)
@@ -114,7 +127,8 @@ funcWithStore(storeWithActionReducerAndPreloadedState)
114127
const storeWithActionReducerAndBadPreloadedState = createStore(
115128
reducerWithAction,
116129
{
117-
b: { c: 'c' }
130+
b: { c: 'c' },
131+
e: brandedString
118132
}
119133
)
120134

@@ -126,7 +140,8 @@ const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(
126140
reducer,
127141
{
128142
a: 'a',
129-
b: { c: 'c', d: 'd' }
143+
b: { c: 'c', d: 'd' },
144+
e: brandedString
130145
},
131146
enhancer
132147
)

0 commit comments

Comments
 (0)