Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make isAnyOf friendly for mapped matchers, but making argument optional
  • Loading branch information
Alex Pimenov authored and markerikson committed Jan 28, 2023
commit f5d7d15758e4a599cbc2bf6df571486b99f59f8a
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)
}