Skip to content

Commit

Permalink
Fix addMatcher typings (#1895)
Browse files Browse the repository at this point in the history
Co-authored-by: Lenz Weber <lenz.weber@mayflower.de>
  • Loading branch information
crcarrick and Lenz Weber committed Jan 21, 2022
1 parent 71277de commit 18c3815
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/toolkit/src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import type { NoInfer } from './tsHelpers'
*/
export type Actions<T extends keyof any = string> = Record<T, Action>

/**
* @deprecated use `TypeGuard` instead
*/
export interface ActionMatcher<A extends AnyAction> {
(action: AnyAction): action is A
}
Expand Down
14 changes: 7 additions & 7 deletions packages/toolkit/src/mapBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { Action, AnyAction } from 'redux'
import type {
CaseReducer,
CaseReducers,
ActionMatcher,
ActionMatcherDescriptionCollection,
} from './createReducer'
import type { TypeGuard } from './tsHelpers'

export interface TypedActionCreator<Type extends string> {
(...args: any[]): Action<Type>
Expand Down Expand Up @@ -96,9 +96,9 @@ const reducer = createReducer(initialState, (builder) => {
});
```
*/
addMatcher<A extends AnyAction>(
matcher: ActionMatcher<A> | ((action: AnyAction) => boolean),
reducer: CaseReducer<State, A>
addMatcher<A>(
matcher: TypeGuard<A> | ((action: any) => boolean),
reducer: CaseReducer<State, A extends AnyAction ? A : A & AnyAction>
): Omit<ActionReducerMapBuilder<State>, 'addCase'>

/**
Expand Down Expand Up @@ -167,9 +167,9 @@ export function executeReducerBuilderCallback<S>(
actionsMap[type] = reducer
return builder
},
addMatcher<A extends AnyAction>(
matcher: ActionMatcher<A>,
reducer: CaseReducer<S, A>
addMatcher<A>(
matcher: TypeGuard<A>,
reducer: CaseReducer<S, A extends AnyAction ? A : A & AnyAction>
) {
if (process.env.NODE_ENV !== 'production') {
if (defaultCaseReducer) {
Expand Down
28 changes: 26 additions & 2 deletions packages/toolkit/src/tests/mapBuilders.typetest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { SerializedError } from '@internal/createAsyncThunk';
import type { SerializedError } from '@internal/createAsyncThunk'
import { createAsyncThunk } from '@internal/createAsyncThunk'
import { executeReducerBuilderCallback } from '@internal/mapBuilders'
import type { AnyAction } from '@reduxjs/toolkit'
import { createAction } from '@reduxjs/toolkit'
import { expectType } from './helpers'
import { expectExactType, expectType } from './helpers'

/** Test: alternative builder callback for actionMap */
{
Expand Down Expand Up @@ -56,10 +56,34 @@ import { expectType } from './helpers'
expectType<ReturnType<typeof increment>>(action)
})

{
// action type is inferred when type predicate lacks `type` property
type PredicateWithoutTypeProperty = {
payload: number
}

builder.addMatcher(
(action): action is PredicateWithoutTypeProperty => true,
(state, action) => {
expectType<PredicateWithoutTypeProperty>(action)
expectType<AnyAction>(action)
}
)
}

// action type defaults to AnyAction if no type predicate matcher is passed
builder.addMatcher(
() => true,
(state, action) => {
expectExactType({} as AnyAction)(action)
}
)

// with a boolean checker, action can also be typed by type argument
builder.addMatcher<{ foo: boolean }>(
() => true,
(state, action) => {
expectType<{ foo: boolean }>(action)
expectType<AnyAction>(action)
}
)
Expand Down
8 changes: 6 additions & 2 deletions packages/toolkit/src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ export type NoInfer<T> = [T][T extends any ? 0 : never]

export type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>

export interface TypeGuard<T> {
(value: any): value is T
}

export interface HasMatchFunction<T> {
match: (v: any) => v is T
match: TypeGuard<T>
}

export const hasMatchFunction = <T>(
Expand All @@ -112,7 +116,7 @@ export const hasMatchFunction = <T>(
}

/** @public */
export type Matcher<T> = HasMatchFunction<T> | ((v: any) => v is T)
export type Matcher<T> = HasMatchFunction<T> | TypeGuard<T>

/** @public */
export type ActionFromMatcher<M extends Matcher<any>> = M extends Matcher<
Expand Down

0 comments on commit 18c3815

Please sign in to comment.