Skip to content

Commit af2d15d

Browse files
committed
Preserve nullable store state type by avoiding intersection with {}
1 parent 7ae6357 commit af2d15d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

packages/toolkit/src/configureStore.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
ExtractDispatchExtensions,
2727
ExtractStoreExtensions,
2828
ExtractStateExtensions,
29+
UnknownIfNonSpecific,
2930
} from './tsHelpers'
3031
import type { Tuple } from './utils'
3132
import type { GetDefaultEnhancers } from './getDefaultEnhancers'
@@ -103,7 +104,8 @@ export type EnhancedStore<
103104
S = any,
104105
A extends Action = UnknownAction,
105106
E extends Enhancers = Enhancers
106-
> = ExtractStoreExtensions<E> & Store<S & ExtractStateExtensions<E>, A>
107+
> = ExtractStoreExtensions<E> &
108+
Store<S, A, UnknownIfNonSpecific<ExtractStateExtensions<E>>>
107109

108110
/**
109111
* A friendly abstraction over the standard Redux `createStore()` function.

packages/toolkit/src/tests/configureStore.typetest.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { PayloadAction, ConfigureStoreOptions } from '@reduxjs/toolkit'
1313
import { configureStore, createSlice, Tuple } from '@reduxjs/toolkit'
1414
import type { ThunkMiddleware, ThunkAction, ThunkDispatch } from 'redux-thunk'
1515
import { thunk } from 'redux-thunk'
16-
import { expectNotAny, expectType } from './helpers'
16+
import { expectExactType, expectNotAny, expectType } from './helpers'
1717

1818
const _anyMiddleware: any = () => () => () => {}
1919

@@ -138,6 +138,17 @@ const _anyMiddleware: any = () => () => () => {}
138138
})
139139
}
140140

141+
/**
142+
* Test: nullable state is preserved
143+
*/
144+
145+
{
146+
const store = configureStore({
147+
reducer: (): string | null => null,
148+
})
149+
expectExactType<string | null>(null)(store.getState())
150+
}
151+
141152
/*
142153
* Test: configureStore() accepts store Tuple, but not plain array
143154
*/

packages/toolkit/src/tsHelpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,5 @@ export type Id<T> = { [K in keyof T]: T[K] } & {}
205205
export type Tail<T extends any[]> = T extends [any, ...infer Tail]
206206
? Tail
207207
: never
208+
209+
export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T

0 commit comments

Comments
 (0)