Skip to content
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
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
],
"dependencies": {
"immer": "^10.0.1",
"redux": "5.0.0-alpha.5",
"redux": "5.0.0-alpha.6",
"redux-thunk": "3.0.0-alpha.3",
"reselect": "^4.1.8"
},
Expand Down
18 changes: 9 additions & 9 deletions packages/toolkit/src/createAction.ts
Original file line number Diff line number Diff line change
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<unknown>) => action is PayloadAction<P, T, M, E>
match: (action: Action<string>) => action is PayloadAction<P, T, M, E>
}

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

actionCreator.type = type

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

return actionCreator
Expand All @@ -290,8 +290,12 @@ export function createAction(type: string, prepareAction?: Function): any {
/**
* Returns true if value is a plain object with a `type` property.
*/
export function isAction(action: unknown): action is Action<unknown> {
return isPlainObject(action) && 'type' in action
export function isAction(action: unknown): action is Action<string> {
return (
isPlainObject(action) &&
'type' in action &&
typeof (action as Record<'type', unknown>).type === 'string'
)
}

/**
Expand All @@ -317,11 +321,7 @@ export function isFSA(action: unknown): action is {
error?: unknown
meta?: unknown
} {
return (
isAction(action) &&
typeof action.type === 'string' &&
Object.keys(action).every(isValidKey)
)
return isAction(action) && Object.keys(action).every(isValidKey)
}

function isValidKey(key: string) {
Expand Down
10 changes: 5 additions & 5 deletions packages/toolkit/src/tests/createAction.typetest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ import { expectType } from './helpers'
// simple use case
{
const actionCreator = createAction<string, 'test'>('test')
const x: Action<unknown> = {} as any
const x: Action<string> = {} as any
if (actionCreator.match(x)) {
expectType<'test'>(x.type)
expectType<string>(x.payload)
Expand All @@ -273,7 +273,7 @@ import { expectType } from './helpers'
// special case: optional argument
{
const actionCreator = createAction<string | undefined, 'test'>('test')
const x: Action<unknown> = {} as any
const x: Action<string> = {} as any
if (actionCreator.match(x)) {
expectType<'test'>(x.type)
expectType<string | undefined>(x.payload)
Expand All @@ -283,7 +283,7 @@ import { expectType } from './helpers'
// special case: without argument
{
const actionCreator = createAction('test')
const x: Action<unknown> = {} as any
const x: Action<string> = {} as any
if (actionCreator.match(x)) {
expectType<'test'>(x.type)
// @ts-expect-error
Expand All @@ -298,7 +298,7 @@ import { expectType } from './helpers'
meta: '',
error: false,
}))
const x: Action<unknown> = {} as any
const x: Action<string> = {} as any
if (actionCreator.match(x)) {
expectType<'test'>(x.type)
expectType<string>(x.payload)
Expand All @@ -315,7 +315,7 @@ import { expectType } from './helpers'
// potential use: as array filter
{
const actionCreator = createAction<string, 'test'>('test')
const x: Array<Action<unknown>> = []
const x: Array<Action<string>> = []
expectType<Array<PayloadAction<string, 'test'>>>(
x.filter(actionCreator.match)
)
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/tests/createSlice.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ const value = actionCreators.anyKey
},
})

const x: Action<unknown> = {} as any
const x: Action<string> = {} as any
if (mySlice.actions.setName.match(x)) {
expectType<'name/setName'>(x.type)
expectType<string>(x.payload)
} else {
// @ts-expect-error
expectType<string>(x.type)
expectType<'name/setName'>(x.type)
// @ts-expect-error
expectType<string>(x.payload)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ describe('serializableStateInvariantMiddleware', () => {
middleware: [serializableStateInvariantMiddleware],
})

const type = Symbol.for('SOME_CONSTANT')
const dispatchedAction = { type }
const symbol = Symbol.for('SOME_CONSTANT')
const dispatchedAction = { type: 'an-action', payload: symbol }

store.dispatch(dispatchedAction)

expect(getLog().log).toMatchInlineSnapshot(`
"A non-serializable value was detected in an action, in the path: \`type\`. Value: Symbol(SOME_CONSTANT)
"A non-serializable value was detected in an action, in the path: \`payload\`. Value: Symbol(SOME_CONSTANT)
Take a look at the logic that dispatched this action: Object {
\\"type\\": Symbol(SOME_CONSTANT),
\\"payload\\": Symbol(SOME_CONSTANT),
\\"type\\": \\"an-action\\",
}
(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants)
(To allow non-serializable values see: https://redux-toolkit.js.org/usage/usage-guide#working-with-non-serializable-data)"
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6784,7 +6784,7 @@ __metadata:
node-fetch: ^2.6.1
prettier: ^2.2.1
query-string: ^7.0.1
redux: 5.0.0-alpha.5
redux: 5.0.0-alpha.6
redux-thunk: 3.0.0-alpha.3
reselect: ^4.1.8
rimraf: ^3.0.2
Expand Down Expand Up @@ -24669,10 +24669,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux@npm:5.0.0-alpha.5":
version: 5.0.0-alpha.5
resolution: "redux@npm:5.0.0-alpha.5"
checksum: 4223be43f605c0d514d5d611a281ae703f905ed4c6014c81b55d1f59cdeac38e3c82fcee2671b102f6b681d95c2a6a9a0f598e044916378011fa0aa39dc644ad
"redux@npm:5.0.0-alpha.6":
version: 5.0.0-alpha.6
resolution: "redux@npm:5.0.0-alpha.6"
checksum: 6f95a75f4c2549dc891700af0aba146fa3f2983bb07918ff47c5635dfdf3b15033d5d1e9981183b291bd70edd59a5842cbc54c6c47d57697c8195f3aef1ffb84
languageName: node
linkType: hard

Expand Down