Skip to content

Commit b360a5a

Browse files
committed
Do not require this for createAction().matches
This has led to a `TypeError` when trying to call `matches()` independently of the action creator object, for instances when passing `actionCreator.matches` to `filter()` without an explicit `bind(actionCreator)`.
1 parent 5f59bd1 commit b360a5a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/createAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function createAction<T extends string | symbol | number>(
2222
type,
2323

2424
matches(a: Action<T>): a is Action<T> {
25-
return this.type === a.type
25+
return a.type == type
2626
},
2727

2828
withPayload<P>(): PayloadActionCreator<P, T> {

test/createAction.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ describe('payload action creator', () => {
5151
expect(incrementBy.matches(decrementBy(1))).toBe(false)
5252
})
5353

54+
test('matches() does not require `this` context', () => {
55+
const incrementBy = createAction('incrementBy').withPayload<number>()
56+
const matches = incrementBy.matches
57+
expect(matches(incrementBy(1))).toBe(true)
58+
})
59+
5460
test('[TypeScript] matches() is a type predicate', () => {
5561
const incrementBy = createAction('incrementBy').withPayload<number>()
5662
const action: Action<unknown> = incrementBy(1)

0 commit comments

Comments
 (0)