()
+
+ console.log(`Fixing \`unique symbol\` exports in ${filePath}`)
+
+ const lines = content.split('\n')
+
+ const allUniqueSymbols = lines
+ .filter((line) => /declare const (\w+)\: unique symbol;/.test(line))
+ .map((line) => line.match(/declare const (\w+)\: unique symbol;/)?.[1])
+
+ if (allUniqueSymbols.length === 0) {
+ console.log(`${filePath} does not have any unique symbols.`)
+
+ return
+ }
+
+ const allNamedExports = lines
+ .at(-2)
+ ?.match(/^export \{ (.*) \};$/)?.[1]
+ .split(', ')
+
+ allNamedExports?.forEach((namedExport) => {
+ if (allUniqueSymbols.includes(namedExport)) {
+ exportedUniqueSymbols.add(namedExport)
+ }
+ })
+
+ if (exportedUniqueSymbols.size === 0) {
+ console.log(
+ `${filePath} has unique symbols but none of them are exported.`,
+ )
+
+ return
+ }
+
+ let newContent = `${lines.slice(0, -2).join('\n')}\nexport { ${allNamedExports?.filter((namedExport) => !exportedUniqueSymbols.has(namedExport)).join(', ')} };\n`
+
+ exportedUniqueSymbols.forEach((uniqueSymbol) => {
+ console.log(`Exporting \`${uniqueSymbol}\` from ${filePath}`)
+
+ newContent = newContent.replace(
+ `declare const ${uniqueSymbol}`,
+ `export declare const ${uniqueSymbol}`,
+ )
+ })
+
+ await fs.writeFile(filePath, newContent)
+ })
+}
+
+main()
diff --git a/packages/toolkit/src/combineSlices.ts b/packages/toolkit/src/combineSlices.ts
index 59166ee5ba..26ff173e7d 100644
--- a/packages/toolkit/src/combineSlices.ts
+++ b/packages/toolkit/src/combineSlices.ts
@@ -1,4 +1,4 @@
-import type { UnknownAction, Reducer, StateFromReducersMapObject } from 'redux'
+import type { Reducer, StateFromReducersMapObject, UnknownAction } from 'redux'
import { combineReducers } from 'redux'
import { nanoid } from './nanoid'
import type {
diff --git a/packages/toolkit/src/createAction.ts b/packages/toolkit/src/createAction.ts
index ea8958288d..4aefdda35f 100644
--- a/packages/toolkit/src/createAction.ts
+++ b/packages/toolkit/src/createAction.ts
@@ -82,7 +82,7 @@ export type _ActionCreatorWithPreparedPayload<
*
* @inheritdoc {redux#ActionCreator}
*/
-export interface BaseActionCreator {
+export type BaseActionCreator
= {
type: T
match: (action: unknown) => action is PayloadAction
}
diff --git a/packages/toolkit/src/createAsyncThunk.ts b/packages/toolkit/src/createAsyncThunk.ts
index e7ee39a05a..4534165869 100644
--- a/packages/toolkit/src/createAsyncThunk.ts
+++ b/packages/toolkit/src/createAsyncThunk.ts
@@ -1,24 +1,16 @@
import type { Dispatch, UnknownAction } from 'redux'
-import type {
- PayloadAction,
- ActionCreatorWithPreparedPayload,
-} from './createAction'
-import { createAction } from './createAction'
import type { ThunkDispatch } from 'redux-thunk'
+import type { ActionCreatorWithPreparedPayload } from './createAction'
+import { createAction } from './createAction'
+import { isAnyOf } from './matchers'
+import { nanoid } from './nanoid'
import type {
- ActionFromMatcher,
FallbackIfUnknown,
Id,
IsAny,
IsUnknown,
SafePromise,
- TypeGuard,
} from './tsHelpers'
-import { nanoid } from './nanoid'
-import { isAnyOf } from './matchers'
-
-// @ts-ignore we need the import of these types due to a bundling issue.
-type _Keep = PayloadAction | ActionCreatorWithPreparedPayload
export type BaseThunkAPI<
S,
@@ -125,11 +117,12 @@ export type AsyncThunkConfig = {
rejectedMeta?: unknown
}
-type GetState = ThunkApiConfig extends {
+export type GetState = ThunkApiConfig extends {
state: infer State
}
? State
: unknown
+
type GetExtra = ThunkApiConfig extends { extra: infer Extra }
? Extra
: unknown
diff --git a/packages/toolkit/src/createSlice.ts b/packages/toolkit/src/createSlice.ts
index 9340bce76a..e3d2c25c56 100644
--- a/packages/toolkit/src/createSlice.ts
+++ b/packages/toolkit/src/createSlice.ts
@@ -1,5 +1,6 @@
-import type { Action, UnknownAction, Reducer } from 'redux'
+import type { Action, Reducer, UnknownAction } from 'redux'
import type { Selector } from 'reselect'
+import type { InjectConfig } from './combineSlices'
import type {
ActionCreatorWithoutPayload,
PayloadAction,
@@ -8,6 +9,14 @@ import type {
_ActionCreatorWithPreparedPayload,
} from './createAction'
import { createAction } from './createAction'
+import type {
+ AsyncThunk,
+ AsyncThunkConfig,
+ AsyncThunkOptions,
+ AsyncThunkPayloadCreator,
+ OverrideThunkApiConfigs,
+} from './createAsyncThunk'
+import { createAsyncThunk as _createAsyncThunk } from './createAsyncThunk'
import type {
ActionMatcherDescriptionCollection,
CaseReducer,
@@ -17,15 +26,6 @@ import { createReducer } from './createReducer'
import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders'
import { executeReducerBuilderCallback } from './mapBuilders'
import type { Id, TypeGuard } from './tsHelpers'
-import type { InjectConfig } from './combineSlices'
-import type {
- AsyncThunk,
- AsyncThunkConfig,
- AsyncThunkOptions,
- AsyncThunkPayloadCreator,
- OverrideThunkApiConfigs,
-} from './createAsyncThunk'
-import { createAsyncThunk as _createAsyncThunk } from './createAsyncThunk'
import { emplace } from './utils'
const asyncThunkSymbol = /* @__PURE__ */ Symbol.for(
@@ -38,7 +38,7 @@ export const asyncThunkCreator: {
[asyncThunkSymbol]: _createAsyncThunk,
}
-interface InjectIntoConfig extends InjectConfig {
+type InjectIntoConfig = InjectConfig & {
reducerPath?: NewReducerPath
}
@@ -135,16 +135,16 @@ export interface Slice<
*
* Selectors can now be called with an `undefined` value, in which case they use the slice's initial state.
*/
-interface InjectedSlice<
+type InjectedSlice<
State = any,
CaseReducers extends SliceCaseReducers = SliceCaseReducers,
Name extends string = string,
ReducerPath extends string = Name,
Selectors extends SliceSelectors = SliceSelectors,
-> extends Omit<
- Slice,
- 'getSelectors' | 'selectors'
- > {
+> = Omit<
+ Slice,
+ 'getSelectors' | 'selectors'
+> & {
/**
* Get localised slice selectors (expects to be called with *just* the slice's state as the first parameter)
*/
@@ -217,8 +217,8 @@ export interface CreateSliceOptions<
/**
* A callback that receives a *builder* object to define
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
- *
- *
+ *
+ *
* @example
```ts
import { createAction, createSlice, Action } from '@reduxjs/toolkit'
@@ -270,15 +270,14 @@ export enum ReducerType {
asyncThunk = 'asyncThunk',
}
-interface ReducerDefinition {
+type ReducerDefinition = {
_reducerDefinitionType: T
}
-export interface CaseReducerDefinition<
+export type CaseReducerDefinition<
S = any,
A extends Action = UnknownAction,
-> extends CaseReducer,
- ReducerDefinition {}
+> = CaseReducer & ReducerDefinition
/**
* A CaseReducer with a `prepare` method.
@@ -296,12 +295,12 @@ export interface CaseReducerWithPrepareDefinition<
> extends CaseReducerWithPrepare,
ReducerDefinition {}
-export interface AsyncThunkSliceReducerConfig<
+type AsyncThunkSliceReducerConfig<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
-> {
+> = {
pending?: CaseReducer<
State,
ReturnType['pending']>
@@ -323,20 +322,15 @@ export interface AsyncThunkSliceReducerConfig<
options?: AsyncThunkOptions
}
-export interface AsyncThunkSliceReducerDefinition<
+type AsyncThunkSliceReducerDefinition<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
-> extends AsyncThunkSliceReducerConfig<
- State,
- ThunkArg,
- Returned,
- ThunkApiConfig
- >,
- ReducerDefinition {
- payloadCreator: AsyncThunkPayloadCreator
-}
+> = AsyncThunkSliceReducerConfig &
+ ReducerDefinition & {
+ payloadCreator: AsyncThunkPayloadCreator
+ }
/**
* Providing these as part of the config would cause circular types, so we disallow passing them
@@ -512,7 +506,7 @@ type ActionCreatorForCaseReducer = CR extends (
*/
type SliceDefinedCaseReducers> = {
[Type in keyof CaseReducers]: CaseReducers[Type] extends infer Definition
- ? Definition extends AsyncThunkSliceReducerDefinition
+ ? Definition extends AsyncThunkSliceReducerDefinition
? Id<
Pick<
Required,
diff --git a/packages/toolkit/src/dynamicMiddleware/index.ts b/packages/toolkit/src/dynamicMiddleware/index.ts
index 2ad15b6c3e..ed151b2979 100644
--- a/packages/toolkit/src/dynamicMiddleware/index.ts
+++ b/packages/toolkit/src/dynamicMiddleware/index.ts
@@ -1,27 +1,28 @@
-import type {
- Middleware,
- Dispatch as ReduxDispatch,
- UnknownAction,
-} from 'redux'
+import type { Dispatch, Middleware, UnknownAction } from 'redux'
import { compose } from 'redux'
import { createAction } from '../createAction'
import { isAllOf } from '../matchers'
import { nanoid } from '../nanoid'
import { emplace, find } from '../utils'
import type {
- WithMiddleware,
AddMiddleware,
- MiddlewareEntry,
DynamicMiddleware,
DynamicMiddlewareInstance,
+ MiddlewareEntry,
+ WithMiddleware,
+} from './types'
+export type {
+ DynamicMiddlewareInstance,
+ GetDispatchType as GetDispatch,
+ MiddlewareApiConfig,
} from './types'
const createMiddlewareEntry = <
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
+ DispatchType extends Dispatch = Dispatch,
>(
- middleware: Middleware,
-): MiddlewareEntry => ({
+ middleware: Middleware,
+): MiddlewareEntry => ({
id: nanoid(),
middleware,
applied: new Map(),
@@ -34,15 +35,15 @@ const matchInstance =
export const createDynamicMiddleware = <
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
->(): DynamicMiddlewareInstance => {
+ DispatchType extends Dispatch = Dispatch,
+>(): DynamicMiddlewareInstance => {
const instanceId = nanoid()
- const middlewareMap = new Map>()
+ const middlewareMap = new Map>()
const withMiddleware = Object.assign(
createAction(
'dynamicMiddleware/add',
- (...middlewares: Middleware[]) => ({
+ (...middlewares: Middleware[]) => ({
payload: middlewares,
meta: {
instanceId,
@@ -50,10 +51,12 @@ export const createDynamicMiddleware = <
}),
),
{ withTypes: () => withMiddleware },
- ) as WithMiddleware
+ ) as WithMiddleware
const addMiddleware = Object.assign(
- function addMiddleware(...middlewares: Middleware[]) {
+ function addMiddleware(
+ ...middlewares: Middleware[]
+ ) {
middlewares.forEach((middleware) => {
let entry = find(
Array.from(middlewareMap.values()),
@@ -66,9 +69,9 @@ export const createDynamicMiddleware = <
})
},
{ withTypes: () => addMiddleware },
- ) as AddMiddleware
+ ) as AddMiddleware
- const getFinalMiddleware: Middleware<{}, State, Dispatch> = (api) => {
+ const getFinalMiddleware: Middleware<{}, State, DispatchType> = (api) => {
const appliedMiddleware = Array.from(middlewareMap.values()).map((entry) =>
emplace(entry.applied, api, { insert: () => entry.middleware(api) }),
)
@@ -77,7 +80,7 @@ export const createDynamicMiddleware = <
const isWithMiddleware = isAllOf(withMiddleware, matchInstance(instanceId))
- const middleware: DynamicMiddleware =
+ const middleware: DynamicMiddleware =
(api) => (next) => (action) => {
if (isWithMiddleware(action)) {
addMiddleware(...action.payload)
diff --git a/packages/toolkit/src/dynamicMiddleware/react/index.ts b/packages/toolkit/src/dynamicMiddleware/react/index.ts
index 27fc179a1b..449bdff459 100644
--- a/packages/toolkit/src/dynamicMiddleware/react/index.ts
+++ b/packages/toolkit/src/dynamicMiddleware/react/index.ts
@@ -1,43 +1,38 @@
import type {
- Action as ReduxAction,
- UnknownAction,
- Dispatch as ReduxDispatch,
- Middleware,
-} from 'redux'
-import type { TSHelpersExtractDispatchExtensions } from '@reduxjs/toolkit'
+ DynamicMiddlewareInstance,
+ GetDispatch,
+ GetState,
+ MiddlewareApiConfig,
+ TSHelpersExtractDispatchExtensions,
+} from '@reduxjs/toolkit'
import { createDynamicMiddleware as cDM } from '@reduxjs/toolkit'
+import type { Context } from 'react'
import type { ReactReduxContextValue } from 'react-redux'
import {
+ createDispatchHook,
ReactReduxContext,
useDispatch as useDefaultDispatch,
- createDispatchHook,
} from 'react-redux'
-import type { Context } from 'react'
-import type {
- DynamicMiddlewareInstance,
- GetDispatch,
- GetState,
- MiddlewareApiConfig,
-} from '@reduxjs/toolkit'
+import type { Action, Dispatch, Middleware, UnknownAction } from 'redux'
export type UseDispatchWithMiddlewareHook<
- Middlewares extends Middleware[] = [],
+ Middlewares extends Middleware[] = [],
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
-> = () => TSHelpersExtractDispatchExtensions & Dispatch
+ DispatchType extends Dispatch = Dispatch,
+> = () => TSHelpersExtractDispatchExtensions & DispatchType
export type CreateDispatchWithMiddlewareHook<
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
+ DispatchType extends Dispatch = Dispatch,
> = {
<
Middlewares extends [
- Middleware,
- ...Middleware[],
+ Middleware,
+ ...Middleware[],
],
>(
...middlewares: Middlewares
- ): UseDispatchWithMiddlewareHook
+ ): UseDispatchWithMiddlewareHook
withTypes<
MiddlewareConfig extends MiddlewareApiConfig,
>(): CreateDispatchWithMiddlewareHook<
@@ -46,35 +41,35 @@ export type CreateDispatchWithMiddlewareHook<
>
}
-type ActionFromDispatch> =
- Dispatch extends ReduxDispatch ? Action : never
+type ActionFromDispatch> =
+ DispatchType extends Dispatch ? Action : never
-interface ReactDynamicMiddlewareInstance<
+type ReactDynamicMiddlewareInstance<
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
-> extends DynamicMiddlewareInstance {
+ DispatchType extends Dispatch = Dispatch,
+> = DynamicMiddlewareInstance & {
createDispatchWithMiddlewareHookFactory: (
context?: Context
+ ActionFromDispatch
> | null>,
- ) => CreateDispatchWithMiddlewareHook
+ ) => CreateDispatchWithMiddlewareHook
createDispatchWithMiddlewareHook: CreateDispatchWithMiddlewareHook<
State,
- Dispatch
+ DispatchType
>
}
export const createDynamicMiddleware = <
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
->(): ReactDynamicMiddlewareInstance => {
- const instance = cDM()
+ DispatchType extends Dispatch = Dispatch,
+>(): ReactDynamicMiddlewareInstance => {
+ const instance = cDM()
const createDispatchWithMiddlewareHookFactory = (
// @ts-ignore
context: Context
+ ActionFromDispatch
> | null> = ReactReduxContext,
) => {
const useDispatch =
@@ -82,7 +77,7 @@ export const createDynamicMiddleware = <
? useDefaultDispatch
: createDispatchHook(context)
function createDispatchWithMiddlewareHook<
- Middlewares extends Middleware[],
+ Middlewares extends Middleware[],
>(...middlewares: Middlewares) {
instance.addMiddleware(...middlewares)
return useDispatch
@@ -91,7 +86,7 @@ export const createDynamicMiddleware = <
createDispatchWithMiddlewareHook
return createDispatchWithMiddlewareHook as CreateDispatchWithMiddlewareHook<
State,
- Dispatch
+ DispatchType
>
}
diff --git a/packages/toolkit/src/dynamicMiddleware/types.ts b/packages/toolkit/src/dynamicMiddleware/types.ts
index 70590afbcb..ee8c37a21b 100644
--- a/packages/toolkit/src/dynamicMiddleware/types.ts
+++ b/packages/toolkit/src/dynamicMiddleware/types.ts
@@ -1,60 +1,50 @@
-import type {
- Middleware,
- Dispatch as ReduxDispatch,
- UnknownAction,
- MiddlewareAPI,
-} from 'redux'
+import type { Dispatch, Middleware, MiddlewareAPI, UnknownAction } from 'redux'
+import type { BaseActionCreator, PayloadAction } from '../createAction'
+import type { GetState } from '../createAsyncThunk'
import type { ExtractDispatchExtensions, FallbackIfUnknown } from '../tsHelpers'
-import type { PayloadAction, BaseActionCreator } from '../createAction'
export type GetMiddlewareApi = MiddlewareAPI<
- GetDispatch,
+ GetDispatchType,
GetState
>
export type MiddlewareApiConfig = {
state?: unknown
- dispatch?: ReduxDispatch
+ dispatch?: Dispatch
}
// TODO: consolidate with cAT helpers?
-export type GetState = MiddlewareApiConfig extends {
- state: infer State
+export type GetDispatchType = MiddlewareApiConfig extends {
+ dispatch: infer DispatchType
}
- ? State
- : unknown
-
-export type GetDispatch = MiddlewareApiConfig extends {
- dispatch: infer Dispatch
-}
- ? FallbackIfUnknown
- : ReduxDispatch
+ ? FallbackIfUnknown
+ : Dispatch
export type AddMiddleware<
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
+ DispatchType extends Dispatch = Dispatch,
> = {
- (...middlewares: Middleware[]): void
+ (...middlewares: Middleware[]): void
withTypes(): AddMiddleware<
GetState,
- GetDispatch
+ GetDispatchType
>
}
-export interface WithMiddleware<
+export type WithMiddleware<
State = any,
- Dispatch extends ReduxDispatch = ReduxDispatch,
-> extends BaseActionCreator<
- Middleware[],
- 'dynamicMiddleware/add',
- { instanceId: string }
- > {
- []>(
+ DispatchType extends Dispatch = Dispatch,
+> = BaseActionCreator<
+ Middleware[],
+ 'dynamicMiddleware/add',
+ { instanceId: string }
+> & {
+ []>(
...middlewares: Middlewares
): PayloadAction
withTypes(): WithMiddleware<
GetState,
- GetDispatch
+ GetDispatchType
>
}
@@ -67,27 +57,27 @@ export interface DynamicDispatch {
export type MiddlewareEntry<
State = unknown,
- Dispatch extends ReduxDispatch = ReduxDispatch,
+ DispatchType extends Dispatch = Dispatch,
> = {
id: string
- middleware: Middleware
+ middleware: Middleware
applied: Map<
- MiddlewareAPI,
- ReturnType>
+ MiddlewareAPI,
+ ReturnType>
>
}
export type DynamicMiddleware<
State = unknown,
- Dispatch extends ReduxDispatch = ReduxDispatch,
-> = Middleware
+ DispatchType extends Dispatch = Dispatch,
+> = Middleware
export type DynamicMiddlewareInstance<
State = unknown,
- Dispatch extends ReduxDispatch = ReduxDispatch,
+ DispatchType extends Dispatch = Dispatch,
> = {
- middleware: DynamicMiddleware
- addMiddleware: AddMiddleware
- withMiddleware: WithMiddleware
+ middleware: DynamicMiddleware
+ addMiddleware: AddMiddleware
+ withMiddleware: WithMiddleware
instanceId: string
}
diff --git a/packages/toolkit/src/entities/models.ts b/packages/toolkit/src/entities/models.ts
index efe06450a9..072b8a8894 100644
--- a/packages/toolkit/src/entities/models.ts
+++ b/packages/toolkit/src/entities/models.ts
@@ -1,8 +1,8 @@
-import type { UncheckedIndexedAccess } from '../uncheckedindexed'
import type { Draft } from 'immer'
import type { PayloadAction } from '../createAction'
+import type { CastAny, Id } from '../tsHelpers'
+import type { UncheckedIndexedAccess } from '../uncheckedindexed.js'
import type { GetSelectorsOptions } from './state_selectors'
-import type { CastAny, Id as Compute } from '../tsHelpers'
/**
* @public
@@ -158,12 +158,12 @@ export interface EntityStateAdapter {
/**
* @public
*/
-export interface EntitySelectors {
- selectIds: (state: V) => Id[]
- selectEntities: (state: V) => Record
+export interface EntitySelectors {
+ selectIds: (state: V) => IdType[]
+ selectEntities: (state: V) => Record
selectAll: (state: V) => T[]
selectTotal: (state: V) => number
- selectById: (state: V, id: Id) => Compute>
+ selectById: (state: V, id: IdType) => Id>
}
/**
diff --git a/packages/toolkit/src/entities/state_selectors.ts b/packages/toolkit/src/entities/state_selectors.ts
index bd64cf001b..2893c99405 100644
--- a/packages/toolkit/src/entities/state_selectors.ts
+++ b/packages/toolkit/src/entities/state_selectors.ts
@@ -1,6 +1,6 @@
-import type { CreateSelectorFunction, Selector, createSelector } from 'reselect'
+import type { CreateSelectorFunction, Selector } from 'reselect'
import { createDraftSafeSelector } from '../createDraftSafeSelector'
-import type { EntityState, EntitySelectors, EntityId } from './models'
+import type { EntityId, EntitySelectors, EntityState } from './models'
type AnyFunction = (...args: any) => any
type AnyCreateSelectorFunction = CreateSelectorFunction<
@@ -8,7 +8,7 @@ type AnyCreateSelectorFunction = CreateSelectorFunction<
(f: F) => F
>
-export interface GetSelectorsOptions {
+export type GetSelectorsOptions = {
createSelector?: AnyCreateSelectorFunction
}
diff --git a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
index 93cff228f6..042ce37218 100644
--- a/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
+++ b/packages/toolkit/src/entities/tests/sorted_state_adapter.test.ts
@@ -1,20 +1,20 @@
-import type { EntityAdapter, EntityState } from '../models'
-import { createEntityAdapter } from '../create_adapter'
+import type { PayloadAction } from '@reduxjs/toolkit'
import {
+ configureStore,
createAction,
createSlice,
- configureStore,
nanoid,
- PayloadAction,
} from '@reduxjs/toolkit'
+import { createNextState } from '../..'
+import { createEntityAdapter } from '../create_adapter'
+import type { EntityAdapter, EntityState } from '../models'
import type { BookModel } from './fixtures/book'
import {
- TheGreatGatsby,
AClockworkOrange,
AnimalFarm,
+ TheGreatGatsby,
TheHobbit,
} from './fixtures/book'
-import { createNextState } from '../..'
describe('Sorted State Adapter', () => {
let adapter: EntityAdapter