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

Update types with PreloadedState generic #3196

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -118,7 +118,7 @@
],
"dependencies": {
"immer": "^9.0.16",
"redux": "5.0.0-alpha.2",
"redux": "Methuselah96/redux#head=preloaded-state-generic-4-with-prepack",
"redux-thunk": "3.0.0-alpha.1",
"reselect": "^4.1.7"
},
Expand Down
23 changes: 15 additions & 8 deletions packages/toolkit/src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type {
StoreEnhancer,
Store,
Dispatch,
PreloadedState,
CombinedState,
} from 'redux'
import { createStore, compose, applyMiddleware, combineReducers } from 'redux'
import type { DevToolsEnhancerOptions as DevToolsOptions } from './devtoolsExtension'
Expand All @@ -21,7 +19,6 @@ import type {
} from './getDefaultMiddleware'
import { curryGetDefaultMiddleware } from './getDefaultMiddleware'
import type {
NoInfer,
ExtractDispatchExtensions,
ExtractStoreExtensions,
} from './tsHelpers'
Expand All @@ -45,14 +42,17 @@ export type ConfigureEnhancersCallback<E extends Enhancers = Enhancers> = (
export interface ConfigureStoreOptions<
S = any,
A extends Action = AnyAction,
PreloadedState = S,
M extends Middlewares<S> = Middlewares<S>,
E extends Enhancers = Enhancers
> {
/**
* A single reducer function that will be used as the root reducer, or an
* object of slice reducers that will be passed to `combineReducers()`.
*/
reducer: Reducer<S, A> | ReducersMapObject<S, A>
reducer:
| Reducer<S, A, PreloadedState>
| ReducersMapObject<S, A, PreloadedState>

/**
* An array of Redux middleware to install. If not supplied, defaults to
Expand Down Expand Up @@ -87,7 +87,7 @@ export interface ConfigureStoreOptions<
As we cannot distinguish between those two cases without adding another generic parameter,
we just make the pragmatic assumption that the latter almost never happens.
*/
preloadedState?: PreloadedState<CombinedState<NoInfer<S>>>
preloadedState?: PreloadedState

/**
* The store enhancers to apply. See Redux's `createStore()`.
Expand Down Expand Up @@ -141,9 +141,12 @@ export type EnhancedStore<
export function configureStore<
S = any,
A extends Action = AnyAction,
PreloadedState = S,
M extends Middlewares<S> = [ThunkMiddlewareFor<S>],
E extends Enhancers = [StoreEnhancer]
>(options: ConfigureStoreOptions<S, A, M, E>): EnhancedStore<S, A, M, E> {
>(
options: ConfigureStoreOptions<S, A, PreloadedState, M, E>
): EnhancedStore<S, A, M, E> {
const curriedGetDefaultMiddleware = curryGetDefaultMiddleware<S>()

const {
Expand All @@ -154,12 +157,16 @@ export function configureStore<
enhancers = undefined,
} = options || {}

let rootReducer: Reducer<S, A>
let rootReducer: Reducer<S, A, PreloadedState>

if (typeof reducer === 'function') {
rootReducer = reducer
} else if (isPlainObject(reducer)) {
rootReducer = combineReducers(reducer) as unknown as Reducer<S, A>
rootReducer = combineReducers(reducer) as unknown as Reducer<
S,
A,
PreloadedState
>
} else {
throw new Error(
'"reducer" is a required argument, and must be a function or an object of functions that can be passed to combineReducers'
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/tests/configureStore.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ const _anyMiddleware: any = () => () => () => {}
})

configureStore({
reducer: () => 0,
// @ts-expect-error
reducer: (_: number) => 0,
preloadedState: 'non-matching state type',
})
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6781,7 +6781,7 @@ __metadata:
node-fetch: ^2.6.1
prettier: ^2.2.1
query-string: ^7.0.1
redux: 5.0.0-alpha.2
redux: "Methuselah96/redux#head=preloaded-state-generic-4-with-prepack"
redux-thunk: 3.0.0-alpha.1
reselect: ^4.1.7
rimraf: ^3.0.2
Expand Down Expand Up @@ -24426,10 +24426,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux@npm:5.0.0-alpha.2":
"redux@Methuselah96/redux#head=preloaded-state-generic-4-with-prepack":
version: 5.0.0-alpha.2
resolution: "redux@npm:5.0.0-alpha.2"
checksum: fbae31c55bab62a210a5a24a64721f593080fb94808c547b646ddff91515f8ab4e3363af5ece16491f0179d43729fc0350235c9fea7500b37b67d762a55b6945
resolution: "redux@https://github.com/Methuselah96/redux.git#commit=93ac9dce5af9813460ce8382ddeae6e27d5471a3"
checksum: 3a075f3b267c5831fa054f38d64b50ee426a554b7feaba91c9a9e4ef5c6ab6292a536955f8c656e66f500f87a493a63587317dc4b48ab207c834def0a99ab5d8
languageName: node
linkType: hard

Expand Down