Skip to content

Commit

Permalink
deprecate ActionMatcher, add TypeGuard type
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenz Weber committed Jan 21, 2022
1 parent afb9ccd commit e1a3231
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/toolkit/src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import type { NoInfer } from './tsHelpers'
*/
export type Actions<T extends keyof any = string> = Record<T, Action>

export interface ActionMatcher<A> {
(action: any): action is A
/**
* @deprecated use `TypeGuard` instead
*/
export interface ActionMatcher<A extends AnyAction> {
(action: AnyAction): action is A
}

export type ActionMatcherDescription<S, A extends AnyAction> = {
Expand Down
6 changes: 3 additions & 3 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 @@ -97,7 +97,7 @@ const reducer = createReducer(initialState, (builder) => {
```
*/
addMatcher<A>(
matcher: ActionMatcher<A> | ((action: any) => boolean),
matcher: TypeGuard<A> | ((action: any) => boolean),
reducer: CaseReducer<State, A extends AnyAction ? A : A & AnyAction>
): Omit<ActionReducerMapBuilder<State>, 'addCase'>

Expand Down Expand Up @@ -168,7 +168,7 @@ export function executeReducerBuilderCallback<S>(
return builder
},
addMatcher<A>(
matcher: ActionMatcher<A>,
matcher: TypeGuard<A>,
reducer: CaseReducer<S, A extends AnyAction ? A : A & AnyAction>
) {
if (process.env.NODE_ENV !== 'production') {
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 e1a3231

Please sign in to comment.