|
3 | 3 | Action,
|
4 | 4 | AnyAction,
|
5 | 5 | Reducer,
|
6 |
| - createStore, |
7 |
| - Store |
| 6 | + createStore |
8 | 7 | } from '../../src'
|
9 | 8 |
|
10 | 9 | interface State {
|
@@ -157,17 +156,23 @@ function replaceReducerExtender() {
|
157 | 156 | }
|
158 | 157 | }
|
159 | 158 |
|
| 159 | + interface PartialState { |
| 160 | + someField?: 'string' |
| 161 | + test?: boolean |
| 162 | + } |
| 163 | + |
| 164 | + const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({ |
| 165 | + someField: 'string' |
| 166 | + }) |
160 | 167 | const store = createStore<
|
161 |
| - { someField?: 'string'; test?: boolean }, |
| 168 | + PartialState, |
162 | 169 | Action<unknown>,
|
163 | 170 | { method(): string },
|
164 | 171 | ExtraState
|
165 |
| - >(reducer, enhancer) |
| 172 | + >(initialReducer, enhancer) |
166 | 173 |
|
167 |
| - const newReducer = ( |
168 |
| - state: { test: boolean } = { test: true }, |
169 |
| - _: AnyAction |
170 |
| - ) => state |
| 174 | + const newReducer = (state: PartialState = { test: true }, _: AnyAction) => |
| 175 | + state |
171 | 176 |
|
172 | 177 | store.replaceReducer(newReducer)
|
173 | 178 | store.getState().test
|
@@ -234,23 +239,27 @@ function mhelmersonExample() {
|
234 | 239 | }
|
235 | 240 | }
|
236 | 241 |
|
237 |
| - const store = createStore< |
238 |
| - { someField?: 'string'; test?: boolean }, |
239 |
| - Action<unknown>, |
240 |
| - {}, |
241 |
| - ExtraState |
242 |
| - >(reducer, enhancer) |
243 |
| - store.replaceReducer(reducer) |
| 242 | + interface PartialState { |
| 243 | + someField?: 'string' |
| 244 | + test?: boolean |
| 245 | + } |
| 246 | + |
| 247 | + const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({ |
| 248 | + someField: 'string' |
| 249 | + }) |
| 250 | + const store = createStore<PartialState, Action<unknown>, {}, ExtraState>( |
| 251 | + initialReducer, |
| 252 | + enhancer |
| 253 | + ) |
| 254 | + store.replaceReducer(initialReducer) |
244 | 255 |
|
245 | 256 | store.getState().extraField
|
246 | 257 | // @ts-expect-error
|
247 | 258 | store.getState().wrongField
|
248 | 259 | store.getState().test
|
249 | 260 |
|
250 |
| - const newReducer = ( |
251 |
| - state: { test: boolean } = { test: true }, |
252 |
| - _: AnyAction |
253 |
| - ) => state |
| 261 | + const newReducer = (state: PartialState = { test: true }, _: AnyAction) => |
| 262 | + state |
254 | 263 |
|
255 | 264 | store.replaceReducer(newReducer)
|
256 | 265 | store.getState().test
|
@@ -304,21 +313,25 @@ function finalHelmersonExample() {
|
304 | 313 | }
|
305 | 314 | }
|
306 | 315 |
|
307 |
| - const store = createStore< |
308 |
| - { someField?: 'string'; test?: boolean }, |
309 |
| - Action<unknown>, |
310 |
| - {}, |
311 |
| - ExtraState |
312 |
| - >(reducer, createPersistEnhancer('hi')) |
| 316 | + interface PartialState { |
| 317 | + someField?: 'string' |
| 318 | + test?: boolean |
| 319 | + } |
| 320 | + |
| 321 | + const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({ |
| 322 | + someField: 'string' |
| 323 | + }) |
| 324 | + const store = createStore<PartialState, Action<unknown>, {}, ExtraState>( |
| 325 | + initialReducer, |
| 326 | + createPersistEnhancer('hi') |
| 327 | + ) |
313 | 328 |
|
314 | 329 | store.getState().foo
|
315 | 330 | // @ts-expect-error
|
316 | 331 | store.getState().wrongField
|
317 | 332 |
|
318 |
| - const newReducer = ( |
319 |
| - state: { test: boolean } = { test: true }, |
320 |
| - _: AnyAction |
321 |
| - ) => state |
| 333 | + const newReducer = (state: PartialState = { test: true }, _: AnyAction) => |
| 334 | + state |
322 | 335 |
|
323 | 336 | store.replaceReducer(newReducer)
|
324 | 337 | store.getState().test
|
|
0 commit comments