Skip to content

Commit f5d7d15

Browse files
Alex Pimenovmarkerikson
Alex Pimenov
authored andcommitted
make isAnyOf friendly for mapped matchers, but making argument optional
1 parent d2d0f70 commit f5d7d15

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

packages/toolkit/src/matchers.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ import type {
1212
} from './createAsyncThunk'
1313

1414
/** @public */
15-
export type ActionMatchingAnyOf<
16-
Matchers extends [Matcher<any>, ...Matcher<any>[]]
17-
> = ActionFromMatcher<Matchers[number]>
15+
export type ActionMatchingAnyOf<Matchers extends [...Matcher<any>[]]> =
16+
ActionFromMatcher<Matchers[number]>
1817

1918
/** @public */
20-
export type ActionMatchingAllOf<
21-
Matchers extends [Matcher<any>, ...Matcher<any>[]]
22-
> = UnionToIntersection<ActionMatchingAnyOf<Matchers>>
19+
export type ActionMatchingAllOf<Matchers extends [...Matcher<any>[]]> =
20+
UnionToIntersection<ActionMatchingAnyOf<Matchers>>
2321

2422
const matches = (matcher: Matcher<any>, action: any) => {
2523
if (hasMatchFunction(matcher)) {
@@ -38,7 +36,7 @@ const matches = (matcher: Matcher<any>, action: any) => {
3836
*
3937
* @public
4038
*/
41-
export function isAnyOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
39+
export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
4240
...matchers: Matchers
4341
) {
4442
return (action: any): action is ActionMatchingAnyOf<Matchers> => {
@@ -55,7 +53,7 @@ export function isAnyOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
5553
*
5654
* @public
5755
*/
58-
export function isAllOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
56+
export function isAllOf<Matchers extends [...Matcher<any>[]]>(
5957
...matchers: Matchers
6058
) {
6159
return (action: any): action is ActionMatchingAllOf<Matchers> => {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,20 @@ function isRejectedWithValueTest(action: AnyAction) {
312312
expectExactType<SerializedError>(action.error)
313313
}
314314
}
315+
316+
function matchersAcceptSpreadArguments() {
317+
const thunk1 = createAsyncThunk('a', () => 'a')
318+
const thunk2 = createAsyncThunk('b', () => 'b')
319+
const interestingThunks = [thunk1, thunk2]
320+
const interestingPendingThunks = interestingThunks.map(
321+
(thunk) => thunk.pending
322+
)
323+
const interestingFulfilledThunks = interestingThunks.map(
324+
(thunk) => thunk.fulfilled
325+
)
326+
327+
const isLoading = isAnyOf(...interestingPendingThunks)
328+
const isNotLoading = isAnyOf(...interestingFulfilledThunks)
329+
330+
const isAllLoading = isAllOf(...interestingPendingThunks)
331+
}

0 commit comments

Comments
 (0)