Skip to content

Commit

Permalink
avoid declare module issue
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Sep 3, 2024
1 parent 90a7737 commit 88fa75f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 51 deletions.
68 changes: 37 additions & 31 deletions packages/toolkit/src/asyncThunkCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,12 @@ import type { CaseReducer } from './createReducer'
import type {
CreatorCaseReducers,
ReducerCreator,
ReducerCreatorEntry,
ReducerDefinition,
} from './createSlice'
import { ReducerType } from './createSlice'
import type { Id } from './tsHelpers'

export type AsyncThunkCreatorExposes<
State,
CaseReducers extends CreatorCaseReducers<State>,
> = {
actions: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
infer ThunkArg,
infer Returned,
infer ThunkApiConfig
>
? AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
: never
}
caseReducers: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
any,
any,
any
>
? Id<
Pick<
Required<CaseReducers[ReducerName]>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>
>
: never
}
}

export type AsyncThunkSliceReducerConfig<
State,
ThunkArg extends any,
Expand Down Expand Up @@ -145,6 +115,42 @@ export interface AsyncThunkCreator<
>
}

export type AsyncThunkCreators<
State,
CaseReducers extends CreatorCaseReducers<State>,
> = {
[ReducerType.asyncThunk]: ReducerCreatorEntry<
AsyncThunkCreator<State>,
{
actions: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
infer ThunkArg,
infer Returned,
infer ThunkApiConfig
>
? AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
: never
}
caseReducers: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
any,
any,
any
>
? Id<
Pick<
Required<CaseReducers[ReducerName]>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>
>
: never
}
}
>
}

export const asyncThunkCreator: ReducerCreator<ReducerType.asyncThunk> = {
type: ReducerType.asyncThunk,
create: /* @__PURE__ */ (() => {
Expand Down
13 changes: 4 additions & 9 deletions packages/toolkit/src/createSlice.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Action, Reducer, UnknownAction } from 'redux'
import type { Selector } from 'reselect'
import type {
AsyncThunkCreator,
AsyncThunkCreatorExposes,
} from './asyncThunkCreator'
import type { AsyncThunkCreators } from './asyncThunkCreator'
import type { InjectConfig } from './combineSlices'
import type {
ActionCreatorWithoutPayload,
Expand All @@ -20,6 +17,7 @@ import type {
ReducerWithInitialState,
} from './createReducer'
import { createReducer, makeGetInitialState } from './createReducer'
import type { EntityCreators } from './entities/slice_creator'
import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders'
import { executeReducerBuilderCallback } from './mapBuilders'
import type {
Expand Down Expand Up @@ -76,7 +74,8 @@ export interface SliceReducerCreators<
CaseReducers extends CreatorCaseReducers<State>,
Name extends string,
ReducerPath extends string,
> {
> extends AsyncThunkCreators<State, CaseReducers>,
EntityCreators<State> {
[ReducerType.reducer]: ReducerCreatorEntry<
{
(
Expand Down Expand Up @@ -142,10 +141,6 @@ export interface SliceReducerCreators<
}
}
>
[ReducerType.asyncThunk]: ReducerCreatorEntry<
AsyncThunkCreator<State>,
AsyncThunkCreatorExposes<State, CaseReducers>
>
}

export type ReducerCreators<
Expand Down
14 changes: 3 additions & 11 deletions packages/toolkit/src/entities/slice_creator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
CaseReducerDefinition,
CreatorCaseReducers,
PayloadAction,
ReducerCreator,
ReducerCreatorEntry,
Expand Down Expand Up @@ -30,7 +29,7 @@ export interface EntityMethodsCreatorConfig<
pluralName?: Plural
}

type EntityMethodsCreator<State> =
export type EntityMethodsCreator<State> =
State extends EntityState<infer T, infer Id>
? {
<
Expand Down Expand Up @@ -69,15 +68,8 @@ type EntityMethodsCreator<State> =
>,
) => EntityReducers<T, Id, State, Single, Plural>

declare module '@reduxjs/toolkit' {
export interface SliceReducerCreators<
State,
CaseReducers extends CreatorCaseReducers<State>,
Name extends string,
ReducerPath extends string,
> {
[entityMethodsCreatorType]: ReducerCreatorEntry<EntityMethodsCreator<State>>
}
export type EntityCreators<State> = {
[entityMethodsCreatorType]: ReducerCreatorEntry<EntityMethodsCreator<State>>
}

const makeWrappedReducerCreator =
Expand Down

0 comments on commit 88fa75f

Please sign in to comment.