From f5d7d15758e4a599cbc2bf6df571486b99f59f8a Mon Sep 17 00:00:00 2001 From: Alex Pimenov Date: Thu, 26 Jan 2023 13:40:19 +0100 Subject: [PATCH] make isAnyOf friendly for mapped matchers, but making argument optional --- packages/toolkit/src/matchers.ts | 14 ++++++-------- packages/toolkit/src/tests/matchers.typetest.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/toolkit/src/matchers.ts b/packages/toolkit/src/matchers.ts index fca137e87d..e8ca651077 100644 --- a/packages/toolkit/src/matchers.ts +++ b/packages/toolkit/src/matchers.ts @@ -12,14 +12,12 @@ import type { } from './createAsyncThunk' /** @public */ -export type ActionMatchingAnyOf< - Matchers extends [Matcher, ...Matcher[]] -> = ActionFromMatcher +export type ActionMatchingAnyOf[]]> = + ActionFromMatcher /** @public */ -export type ActionMatchingAllOf< - Matchers extends [Matcher, ...Matcher[]] -> = UnionToIntersection> +export type ActionMatchingAllOf[]]> = + UnionToIntersection> const matches = (matcher: Matcher, action: any) => { if (hasMatchFunction(matcher)) { @@ -38,7 +36,7 @@ const matches = (matcher: Matcher, action: any) => { * * @public */ -export function isAnyOf, ...Matcher[]]>( +export function isAnyOf[]]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAnyOf => { @@ -55,7 +53,7 @@ export function isAnyOf, ...Matcher[]]>( * * @public */ -export function isAllOf, ...Matcher[]]>( +export function isAllOf[]]>( ...matchers: Matchers ) { return (action: any): action is ActionMatchingAllOf => { diff --git a/packages/toolkit/src/tests/matchers.typetest.ts b/packages/toolkit/src/tests/matchers.typetest.ts index e2b6ee8bf1..8770983f01 100644 --- a/packages/toolkit/src/tests/matchers.typetest.ts +++ b/packages/toolkit/src/tests/matchers.typetest.ts @@ -312,3 +312,20 @@ function isRejectedWithValueTest(action: AnyAction) { expectExactType(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) +}