Skip to content

Commit 8c1c6f0

Browse files
committed
strongly type action types
1 parent c8daec5 commit 8c1c6f0

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

packages/toolkit/src/tests/createSlice.test-d.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
ReducerHandlingContextMethods,
2020
ReducerNamesOfType,
2121
SerializedError,
22+
SliceActionType,
2223
SliceCaseReducers,
2324
ThunkAction,
2425
ThunkDispatch,
@@ -1032,17 +1033,23 @@ describe('type tests', () => {
10321033
})
10331034

10341035
const toastSlice = createAppSlice({
1035-
name: 'toast',
1036+
name: 'toasts',
10361037
initialState: { toasts: {} } as ToastState,
10371038
reducers: (create) => ({
10381039
toast: create.toaster(),
10391040
}),
10401041
})
10411042

1042-
expectTypeOf(toastSlice.actions.toast).toEqualTypeOf<AddToastThunk>()
1043+
expectTypeOf(toastSlice.actions.toast).toEqualTypeOf<
1044+
AddToastThunk<'toasts', 'toast'>
1045+
>()
10431046

10441047
expectTypeOf(toastSlice.actions.toast).toBeCallableWith(100, 'hello')
10451048

1049+
expectTypeOf(
1050+
toastSlice.actions.toast.toastOpened.type,
1051+
).toEqualTypeOf<'toasts/toast/opened'>()
1052+
10461053
const incompatibleSlice = createAppSlice({
10471054
name: 'incompatible',
10481055
initialState: {},
@@ -1062,13 +1069,19 @@ interface ToastState {
10621069
toasts: Record<string, Toast>
10631070
}
10641071

1065-
interface AddToastThunk {
1072+
interface AddToastThunk<Name extends string, ReducerName extends PropertyKey> {
10661073
(
10671074
ms: number,
10681075
message: string,
10691076
): ThunkAction<void, unknown, unknown, UnknownAction>
1070-
toastOpened: PayloadActionCreator<{ message: string; id: string }>
1071-
toastClosed: PayloadActionCreator<string>
1077+
toastOpened: PayloadActionCreator<
1078+
{ message: string; id: string },
1079+
`${SliceActionType<Name, ReducerName>}/opened`
1080+
>
1081+
toastClosed: PayloadActionCreator<
1082+
string,
1083+
`${SliceActionType<Name, ReducerName>}/closed`
1084+
>
10721085
}
10731086

10741087
declare module '@reduxjs/toolkit' {
@@ -1087,7 +1100,7 @@ declare module '@reduxjs/toolkit' {
10871100
[ReducerName in ReducerNamesOfType<
10881101
CaseReducers,
10891102
typeof toasterCreatorType
1090-
>]: AddToastThunk
1103+
>]: AddToastThunk<Name, ReducerName>
10911104
}
10921105
}
10931106
>

packages/toolkit/src/tests/createSlice.test.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,23 @@ interface LoaderReducerDefinition<State>
12101210
ended?: CaseReducer<State, PayloadAction<string>>
12111211
}
12121212

1213+
interface LoaderThunk<Name extends string, ReducerName extends PropertyKey> {
1214+
(): ThunkAction<
1215+
{ loaderId: string; end: () => void },
1216+
unknown,
1217+
unknown,
1218+
Action
1219+
>
1220+
started: PayloadActionCreator<
1221+
string,
1222+
`${SliceActionType<Name, ReducerName>}/started`
1223+
>
1224+
ended: PayloadActionCreator<
1225+
string,
1226+
`${SliceActionType<Name, ReducerName>}/ended`
1227+
>
1228+
}
1229+
12131230
interface PatchesState {
12141231
undo: Patch[]
12151232
redo: Patch[]
@@ -1241,21 +1258,7 @@ declare module '@reduxjs/toolkit' {
12411258
[ReducerName in ReducerNamesOfType<
12421259
CaseReducers,
12431260
typeof loaderCreatorType
1244-
>]: (() => ThunkAction<
1245-
{ loaderId: string; end: () => void },
1246-
unknown,
1247-
unknown,
1248-
Action
1249-
>) & {
1250-
started: PayloadActionCreator<
1251-
string,
1252-
`${SliceActionType<Name, ReducerName>}/started`
1253-
>
1254-
ended: PayloadActionCreator<
1255-
string,
1256-
`${SliceActionType<Name, ReducerName>}/ended`
1257-
>
1258-
}
1261+
>]: LoaderThunk<Name, ReducerName>
12591262
}
12601263
caseReducers: {
12611264
[ReducerName in ReducerNamesOfType<

0 commit comments

Comments
 (0)