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

Fix addMatcher typings #1895

Merged
merged 3 commits into from
Jan 21, 2022
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
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