Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve nullable store state type by avoiding intersection with {} #4011

Merged
merged 3 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
],
"dependencies": {
"immer": "^10.0.3",
"redux": "^5.0.0",
"redux": "^5.0.1",
"redux-thunk": "^3.1.0",
"reselect": "^5.0.1"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/toolkit/src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
ExtractDispatchExtensions,
ExtractStoreExtensions,
ExtractStateExtensions,
UnknownIfNonSpecific,
} from './tsHelpers'
import type { Tuple } from './utils'
import type { GetDefaultEnhancers } from './getDefaultEnhancers'
Expand Down Expand Up @@ -103,7 +104,8 @@ export type EnhancedStore<
S = any,
A extends Action = UnknownAction,
E extends Enhancers = Enhancers
> = ExtractStoreExtensions<E> & Store<S & ExtractStateExtensions<E>, A>
> = ExtractStoreExtensions<E> &
Store<S, A, UnknownIfNonSpecific<ExtractStateExtensions<E>>>

/**
* A friendly abstraction over the standard Redux `createStore()` function.
Expand Down
13 changes: 12 additions & 1 deletion packages/toolkit/src/tests/configureStore.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { PayloadAction, ConfigureStoreOptions } from '@reduxjs/toolkit'
import { configureStore, createSlice, Tuple } from '@reduxjs/toolkit'
import type { ThunkMiddleware, ThunkAction, ThunkDispatch } from 'redux-thunk'
import { thunk } from 'redux-thunk'
import { expectNotAny, expectType } from './helpers'
import { expectExactType, expectNotAny, expectType } from './helpers'

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

Expand Down Expand Up @@ -138,6 +138,17 @@ const _anyMiddleware: any = () => () => () => {}
})
}

/**
* Test: nullable state is preserved
*/

{
const store = configureStore({
reducer: (): string | null => null,
})
expectExactType<string | null>(null)(store.getState())
}

/*
* Test: configureStore() accepts store Tuple, but not plain array
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ export type Id<T> = { [K in keyof T]: T[K] } & {}
export type Tail<T extends any[]> = T extends [any, ...infer Tail]
? Tail
: never

export type UnknownIfNonSpecific<T> = {} extends T ? unknown : T
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7075,7 +7075,7 @@ __metadata:
node-fetch: ^2.6.1
prettier: ^2.2.1
query-string: ^7.0.1
redux: ^5.0.0
redux: ^5.0.1
redux-thunk: ^3.1.0
reselect: ^5.0.1
rimraf: ^3.0.2
Expand Down Expand Up @@ -25152,10 +25152,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux@npm:^5.0.0":
version: 5.0.0
resolution: "redux@npm:5.0.0"
checksum: be49160d4bd01e10108c425ade999f1b456204895c4bdd0c7825ab09efffded51955c5c242847406a7b3f273e9011a9c102848c512a099a75617b97b13d2cca8
"redux@npm:^5.0.1":
version: 5.0.1
resolution: "redux@npm:5.0.1"
checksum: e74affa9009dd5d994878b9a1ce30d6569d986117175056edb003de2651c05b10fe7819d6fa94aea1a94de9a82f252f986547f007a2fbeb35c317a2e5f5ecf2c
languageName: node
linkType: hard

Expand Down