diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index 6a5ccea09f..6cb6d194d6 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -12,11 +12,11 @@ import type { } from './createAsyncThunk' /** @public */ -export type ActionMatchingAnyOf[]]> = +export type ActionMatchingAnyOf[]> = ActionFromMatcher /** @public */ -export type ActionMatchingAllOf[]]> = +export type ActionMatchingAllOf[]> = UnionToIntersection> const matches = (matcher: Matcher, action: any) => { @@ -36,7 +36,7 @@ const matches = (matcher: Matcher, action: any) => { * * @public */ -export function isAnyOf[]]>( +export function isAnyOf[]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAnyOf => { @@ -53,7 +53,7 @@ export function isAnyOf[]]>( * * @public */ -export function isAllOf[]]>( +export function isAllOf[]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAllOf => { @@ -136,18 +136,7 @@ export function isPending< return isPending()(asyncThunks[0]) } - return ( - action: any, - ): action is PendingActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = asyncThunks.map( - (asyncThunk) => asyncThunk.pending, - ) as any - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) - } + return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.pending)) } export type UnknownAsyncThunkRejectedAction = ReturnType< @@ -199,18 +188,7 @@ export function isRejected< return isRejected()(asyncThunks[0]) } - return ( - action: any, - ): action is RejectedActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = asyncThunks.map( - (asyncThunk) => asyncThunk.rejected, - ) as any - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) - } + return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.rejected)) } export type UnknownAsyncThunkRejectedWithValueAction = ReturnType< @@ -264,24 +242,14 @@ export function isRejectedWithValue< } if (asyncThunks.length === 0) { - return (action: any) => { - const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag) - - return combinedMatcher(action) - } + return isAllOf(isRejected(...asyncThunks), hasFlag) } if (!isAsyncThunkArray(asyncThunks)) { return isRejectedWithValue()(asyncThunks[0]) } - return ( - action: any, - ): action is RejectedActionFromAsyncThunk => { - const combinedMatcher = isAllOf(isRejected(...asyncThunks), hasFlag) - - return combinedMatcher(action) - } + return isAllOf(isRejected(...asyncThunks), hasFlag) } export type UnknownAsyncThunkFulfilledAction = ReturnType< @@ -333,18 +301,7 @@ export function isFulfilled< return isFulfilled()(asyncThunks[0]) } - return ( - action: any, - ): action is FulfilledActionFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = asyncThunks.map( - (asyncThunk) => asyncThunk.fulfilled, - ) as any - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) - } + return isAnyOf(...asyncThunks.map((asyncThunk) => asyncThunk.fulfilled)) } export type UnknownAsyncThunkAction = @@ -404,22 +361,5 @@ export function isAsyncThunkAction< return isAsyncThunkAction()(asyncThunks[0]) } - return ( - action: any, - ): action is ActionsFromAsyncThunk => { - // note: this type will be correct because we have at least 1 asyncThunk - const matchers: [Matcher, ...Matcher[]] = [] as any - - for (const asyncThunk of asyncThunks) { - matchers.push( - asyncThunk.pending, - asyncThunk.rejected, - asyncThunk.fulfilled, - ) - } - - const combinedMatcher = isAnyOf(...matchers) - - return combinedMatcher(action) - } + return isAnyOf(...asyncThunks.flatMap(asyncThunk => [asyncThunk.pending, asyncThunk.rejected, asyncThunk.fulfilled])) }