Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make isAnyOf friendly for mapped matchers, but making argument optional #3123

Merged
merged 1 commit into from
Jan 28, 2023
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
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)
}