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

Use isAction inside .match so it can handle unknown #3897

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 4 additions & 4 deletions packages/toolkit/src/createAction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Action, UnknownAction } from 'redux'
import type { Action } from 'redux'
import type {
IsUnknownOrNonInferrable,
IfMaybeUndefined,
Expand Down Expand Up @@ -84,7 +84,7 @@ export type _ActionCreatorWithPreparedPayload<
*/
export interface BaseActionCreator<P, T extends string, M = never, E = never> {
type: T
match: (action: Action<string>) => action is PayloadAction<P, T, M, E>
match: (action: unknown) => action is PayloadAction<P, T, M, E>
}

/**
Expand Down Expand Up @@ -279,8 +279,8 @@ export function createAction(type: string, prepareAction?: Function): any {

actionCreator.type = type

actionCreator.match = (action: Action<string>): action is PayloadAction =>
action.type === type
actionCreator.match = (action: unknown): action is PayloadAction =>
isAction(action) && action.type === type

return actionCreator
}
Expand Down
8 changes: 2 additions & 6 deletions packages/toolkit/src/dynamicMiddleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
UnknownAction,
} from 'redux'
import { compose } from 'redux'
import { createAction, isAction } from '../createAction'
import { createAction } from '../createAction'
import { isAllOf } from '../matchers'
import { nanoid } from '../nanoid'
import { emplace, find } from '../utils'
Expand Down Expand Up @@ -75,11 +75,7 @@ export const createDynamicMiddleware = <
return compose(...appliedMiddleware)
}

const isWithMiddleware = isAllOf(
isAction,
withMiddleware,
matchInstance(instanceId)
)
const isWithMiddleware = isAllOf(withMiddleware, matchInstance(instanceId))

const middleware: DynamicMiddleware<State, Dispatch> =
(api) => (next) => (action) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/toolkit/src/dynamicMiddleware/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Middleware } from 'redux'
import { createDynamicMiddleware } from '../index'
import { configureStore } from '../../configureStore'
import type { BaseActionCreator, PayloadAction } from '../../createAction'
import { isAction } from '../../createAction'
import { createAction } from '../../createAction'
import { isAllOf } from '../../matchers'

Expand All @@ -25,7 +24,7 @@ export const makeProbeableMiddleware = <Id extends number>(
): Middleware<{
(action: PayloadAction<Id, typeof probeType>): Id
}> => {
const isMiddlewareAction = isAllOf(isAction, probeMiddleware, matchId(id))
const isMiddlewareAction = isAllOf(probeMiddleware, matchId(id))
return (api) => (next) => (action) => {
if (isMiddlewareAction(action)) {
return id
Expand Down
4 changes: 0 additions & 4 deletions packages/toolkit/src/tests/createAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ class Action {
}
describe('isAction', () => {
it('should only return true for plain objects with a string type property', () => {
const actionCreator = createAction('anAction')
class Action {
type = 'totally an action'
}
const testCases: [action: unknown, expected: boolean][] = [
[{ type: 'an action' }, true],
[{ type: 'more props', extra: true }, true],
Expand Down