Skip to content

Commit 71240c9

Browse files
committed
Merge branch 'master' into feature/entities
2 parents a1d4091 + 7e02ab4 commit 71240c9

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

docs/tutorials/intermediate-tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default todos
146146
We can see that it handles three cases:
147147

148148
- It adds a new todo by copying the existing `state` array and adding a new todo entry at the end
149-
- It handles toggling a todo entry by copying the existing array use `state.map()`, copies and replaces the todo object that needs to be updated, and leaves all other todo entries alone.
149+
- It handles toggling a todo entry by copying the existing array using `state.map()`, copies and replaces the todo object that needs to be updated, and leaves all other todo entries alone.
150150
- It responds to all other actions by returning the existing state (effectively saying "I don't care about that action").
151151

152152
It also initializes the state with a default value of `[]`, and does a default export of the reducer function.

etc/redux-toolkit.api.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export type SliceActionCreator<P> = PayloadActionCreator<P>;
261261

262262
// @public
263263
export type SliceCaseReducers<State> = {
264-
[K: string]: CaseReducer<State, PayloadAction<any>> | CaseReducerWithPrepare<State, PayloadAction<any>>;
264+
[K: string]: CaseReducer<State, PayloadAction<any>> | CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>;
265265
};
266266

267267
export { ThunkAction }
@@ -271,14 +271,10 @@ export type Update<T> = UpdateStr<T> | UpdateNum<T>;
271271

272272
// @public
273273
export type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & {
274-
[P in keyof ACR]: ACR[P] extends {
275-
reducer(s: S, action?: {
276-
payload: infer O;
277-
}): any;
274+
[T in keyof ACR]: ACR[T] extends {
275+
reducer(s: S, action?: infer A): any;
278276
} ? {
279-
prepare(...a: never[]): {
280-
payload: O;
281-
};
277+
prepare(...a: never[]): Omit<A, 'type'>;
282278
} : {};
283279
};
284280

src/createSlice.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ActionReducerMapBuilder,
1313
executeReducerBuilderCallback
1414
} from './mapBuilders'
15+
import { Omit } from './tsHelpers'
1516

1617
/**
1718
* An action creator atttached to a slice.
@@ -110,7 +111,7 @@ export type CaseReducerWithPrepare<State, Action extends PayloadAction> = {
110111
export type SliceCaseReducers<State> = {
111112
[K: string]:
112113
| CaseReducer<State, PayloadAction<any>>
113-
| CaseReducerWithPrepare<State, PayloadAction<any>>
114+
| CaseReducerWithPrepare<State, PayloadAction<any, string, any, any>>
114115
}
115116

116117
/**
@@ -187,11 +188,11 @@ export type ValidateSliceCaseReducers<
187188
ACR extends SliceCaseReducers<S>
188189
> = ACR &
189190
{
190-
[P in keyof ACR]: ACR[P] extends {
191-
reducer(s: S, action?: { payload: infer O }): any
191+
[T in keyof ACR]: ACR[T] extends {
192+
reducer(s: S, action?: infer A): any
192193
}
193194
? {
194-
prepare(...a: never[]): { payload: O }
195+
prepare(...a: never[]): Omit<A, 'type'>
195196
}
196197
: {}
197198
}

src/tsHelpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ type UnionToIntersection<U> = (U extends any
8787
: never) extends ((k: infer I) => void)
8888
? I
8989
: never
90+
91+
export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>

type-tests/files/createSlice.typetest.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function expectType<T>(t: T) {
7070
? payload.reduce((acc, val) => acc * val, state)
7171
: state * payload,
7272
addTwo: {
73-
reducer: (s, { payload }) => s + payload,
73+
reducer: (s, { payload }: PayloadAction<number>) => s + payload,
7474
prepare: (a: number, b: number) => ({
7575
payload: a + b
7676
})
@@ -175,6 +175,65 @@ function expectType<T>(t: T) {
175175
expectType<string>(counter.actions.concatMetaStrLen('test').meta)
176176
}
177177

178+
/**
179+
* Test: access meta and error from reducer
180+
*/
181+
{
182+
const counter = createSlice({
183+
name: 'test',
184+
initialState: { counter: 0, concat: '' },
185+
reducers: {
186+
// case: meta and error not used in reducer
187+
testDefaultMetaAndError: {
188+
reducer(_, action: PayloadAction<number, string>) {},
189+
prepare: (payload: number) => ({
190+
payload,
191+
meta: 'meta' as 'meta',
192+
error: 'error' as 'error'
193+
})
194+
},
195+
// case: meta and error marked as "unknown" in reducer
196+
testUnknownMetaAndError: {
197+
reducer(_, action: PayloadAction<number, string, unknown, unknown>) {},
198+
prepare: (payload: number) => ({
199+
payload,
200+
meta: 'meta' as 'meta',
201+
error: 'error' as 'error'
202+
})
203+
},
204+
// case: meta and error are typed in the reducer as returned by prepare
205+
testMetaAndError: {
206+
reducer(_, action: PayloadAction<number, string, 'meta', 'error'>) {},
207+
prepare: (payload: number) => ({
208+
payload,
209+
meta: 'meta' as 'meta',
210+
error: 'error' as 'error'
211+
})
212+
},
213+
// case: meta is typed differently in the reducer than returned from prepare
214+
testErroneousMeta: {
215+
reducer(_, action: PayloadAction<number, string, 'meta', 'error'>) {},
216+
// typings:expect-error
217+
prepare: (payload: number) => ({
218+
payload,
219+
meta: 1,
220+
error: 'error' as 'error'
221+
})
222+
},
223+
// case: error is typed differently in the reducer than returned from prepare
224+
testErroneousError: {
225+
reducer(_, action: PayloadAction<number, string, 'meta', 'error'>) {},
226+
// typings:expect-error
227+
prepare: (payload: number) => ({
228+
payload,
229+
meta: 'meta' as 'meta',
230+
error: 1
231+
})
232+
}
233+
}
234+
})
235+
}
236+
178237
/*
179238
* Test: returned case reducer has the correct type
180239
*/

0 commit comments

Comments
 (0)