Skip to content

Commit a15d6e8

Browse files
author
ben.durrant
committed
reuse utility and test preparedReducer
1 parent 0ef81e7 commit a15d6e8

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

packages/toolkit/src/createAction.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,6 @@ export type PrepareAction<P> =
5050
| ((...args: any[]) => { payload: P; error: any })
5151
| ((...args: any[]) => { payload: P; meta: any; error: any })
5252

53-
export type _PayloadActionForPrepare<
54-
PA extends PrepareAction<any>,
55-
T extends string = string
56-
> = PA extends PrepareAction<infer P>
57-
? PayloadAction<
58-
P,
59-
T,
60-
ReturnType<PA> extends {
61-
meta: infer M
62-
}
63-
? M
64-
: never,
65-
ReturnType<PA> extends {
66-
error: infer E
67-
}
68-
? E
69-
: never
70-
>
71-
: never
72-
7353
/**
7454
* Internal version of `ActionCreatorWithPreparedPayload`. Not to be used externally.
7555
*

packages/toolkit/src/createSlice.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
PayloadActionCreator,
66
PrepareAction,
77
_ActionCreatorWithPreparedPayload,
8-
_PayloadActionForPrepare,
98
} from './createAction'
109
import { createAction } from './createAction'
1110
import type {
@@ -373,11 +372,17 @@ interface ReducerCreators<State> {
373372

374373
preparedReducer<Prepare extends PrepareAction<any>>(
375374
prepare: Prepare,
376-
reducer: CaseReducer<State, _PayloadActionForPrepare<Prepare>>
375+
reducer: CaseReducer<
376+
State,
377+
ReturnType<_ActionCreatorWithPreparedPayload<Prepare>>
378+
>
377379
): {
378380
[reducerDefinitionType]: ReducerType.reducerWithPrepare
379381
prepare: Prepare
380-
reducer: CaseReducer<State, _PayloadActionForPrepare<Prepare>>
382+
reducer: CaseReducer<
383+
State,
384+
ReturnType<_ActionCreatorWithPreparedPayload<Prepare>>
385+
>
381386
}
382387
}
383388

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
} from '@reduxjs/toolkit'
1818
import { configureStore } from '@reduxjs/toolkit'
1919
import { createAction, createSlice } from '@reduxjs/toolkit'
20-
import { expectType, expectUnknown } from './helpers'
20+
import { expectExactType, expectType, expectUnknown } from './helpers'
2121

2222
/*
2323
* Test: Slice name is strongly typed.
@@ -597,6 +597,19 @@ const value = actionCreators.anyKey
597597
expectType<TestState>(state)
598598
expectType<string>(action.payload)
599599
}),
600+
preparedReducer: create.preparedReducer(
601+
(payload: string) => ({
602+
payload,
603+
meta: 'meta' as const,
604+
error: 'error' as const,
605+
}),
606+
(state, action) => {
607+
expectType<TestState>(state)
608+
expectType<string>(action.payload)
609+
expectExactType('meta' as const)(action.meta)
610+
expectExactType('error' as const)(action.error)
611+
}
612+
),
600613
testInfer: create.asyncThunk(
601614
function payloadCreator(arg: TestArg, api) {
602615
return Promise.resolve<TestReturned>({ payload: 'foo' })
@@ -688,6 +701,15 @@ const value = actionCreators.anyKey
688701
type StoreDispatch = typeof store.dispatch
689702

690703
expectType<PayloadActionCreator<string>>(slice.actions.normalReducer)
704+
expectType<
705+
ActionCreatorWithPreparedPayload<
706+
[string],
707+
string,
708+
'test/preparedReducer',
709+
'error',
710+
'meta'
711+
>
712+
>(slice.actions.preparedReducer)
691713
expectType<AsyncThunk<TestReturned, TestArg, {}>>(slice.actions.testInfer)
692714
expectType<AsyncThunk<TestReturned, TestArg, { rejectValue: TestReject }>>(
693715
slice.actions.testExplicitType

0 commit comments

Comments
 (0)