Skip to content

Commit

Permalink
make isAnyOf friendly for mapped matchers, but making argument optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Pimenov authored and markerikson committed Jan 28, 2023
1 parent d2d0f70 commit f5d7d15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/toolkit/src/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import type {
} from './createAsyncThunk'

/** @public */
export type ActionMatchingAnyOf<
Matchers extends [Matcher<any>, ...Matcher<any>[]]
> = ActionFromMatcher<Matchers[number]>
export type ActionMatchingAnyOf<Matchers extends [...Matcher<any>[]]> =
ActionFromMatcher<Matchers[number]>

/** @public */
export type ActionMatchingAllOf<
Matchers extends [Matcher<any>, ...Matcher<any>[]]
> = UnionToIntersection<ActionMatchingAnyOf<Matchers>>
export type ActionMatchingAllOf<Matchers extends [...Matcher<any>[]]> =
UnionToIntersection<ActionMatchingAnyOf<Matchers>>

const matches = (matcher: Matcher<any>, action: any) => {
if (hasMatchFunction(matcher)) {
Expand All @@ -38,7 +36,7 @@ const matches = (matcher: Matcher<any>, action: any) => {
*
* @public
*/
export function isAnyOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
export function isAnyOf<Matchers extends [...Matcher<any>[]]>(
...matchers: Matchers
) {
return (action: any): action is ActionMatchingAnyOf<Matchers> => {
Expand All @@ -55,7 +53,7 @@ export function isAnyOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
*
* @public
*/
export function isAllOf<Matchers extends [Matcher<any>, ...Matcher<any>[]]>(
export function isAllOf<Matchers extends [...Matcher<any>[]]>(
...matchers: Matchers
) {
return (action: any): action is ActionMatchingAllOf<Matchers> => {
Expand Down
17 changes: 17 additions & 0 deletions packages/toolkit/src/tests/matchers.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,20 @@ function isRejectedWithValueTest(action: AnyAction) {
expectExactType<SerializedError>(action.error)
}
}

function matchersAcceptSpreadArguments() {
const thunk1 = createAsyncThunk('a', () => 'a')
const thunk2 = createAsyncThunk('b', () => 'b')
const interestingThunks = [thunk1, thunk2]
const interestingPendingThunks = interestingThunks.map(
(thunk) => thunk.pending
)
const interestingFulfilledThunks = interestingThunks.map(
(thunk) => thunk.fulfilled
)

const isLoading = isAnyOf(...interestingPendingThunks)
const isNotLoading = isAnyOf(...interestingFulfilledThunks)

const isAllLoading = isAllOf(...interestingPendingThunks)
}

0 comments on commit f5d7d15

Please sign in to comment.