Skip to content

fix(toolkit): flag rejectedWithValue for falsy rejectWithValue payloads - #5395

Merged
markerikson merged 1 commit into
reduxjs:masterfrom
dfedoryshchev:fix/rejectedwithvalue-falsy-payload
Sep 19, 2026
Merged

markerikson merged 1 commit into
reduxjs:masterfrom
dfedoryshchev:fix/rejectedwithvalue-falsy-payload

Conversation

@dfedoryshchev

Copy link
Copy Markdown
Contributor

Problem

rejectWithValue(0), rejectWithValue(''), rejectWithValue(false) and rejectWithValue(null) produce a rejected action that carries the payload but is flagged rejectedWithValue: false.

// createAsyncThunk.ts:593
rejectedWithValue: !!payload,

Three consumers then treat a deliberately handled rejection as an unhandled one: unwrap() throws the generic { message: 'Rejected' } rather than the value (createAsyncThunk.ts:784), isRejectedWithValue() does not match (matchers.ts:241), and RTK Query's queryFulfilled rejects with isUnhandledError: true (queryLifecycle.ts:497-500).

RTK Query reaches it without anything unusual: buildThunks.ts:839 passes whatever an endpoint's transformErrorResponse returns straight into rejectWithValue, so a transform that returns an empty string on an error response is enough.

The type already treats this as a presence discriminant rather than a truthiness one - createAsyncThunk.ts:407-412 is a union on { rejectedWithValue: false } vs { rejectedWithValue: true } & GetRejectedMeta - and the docs say rejectWithValue passes "whatever value you give it".

Fix

rejectedWithValue: payload !== undefined.

One residual gap I left alone deliberately: rejectWithValue(undefined) still flags false, because the rejected prepare callback cannot distinguish a fourth argument of undefined from an absent one without an arity check, and that would change behaviour for anyone calling thunk.rejected(err, id, arg, undefined) directly. Happy to switch to the arity version if you would rather cover it.

Tests

Four cases across the falsy values plus an unwrapResult case, added next to the existing rejectWithValue tests. packages/toolkit goes 1067 to 1072 passing with no type errors; the suite has no pre-existing failures either way.

Related to #4725, though I cannot confirm which falsy value that reporter's transformErrorResponse produced, so I am not claiming it as closed.

@codesandbox

codesandbox Bot commented Aug 27, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web Editor • VS Code • Insiders

Open Preview

@pkg-pr-new

pkg-pr-new Bot commented Aug 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

npm i https://pkg.pr.new/@reduxjs/rtk-codemods@60b02f1 -D
yarn add https://pkg.pr.new/@reduxjs/rtk-codemods@60b02f1.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/rtk-codemods@60b02f1.tgz -D
bun add https://pkg.pr.new/@reduxjs/rtk-codemods@60b02f1.tgz -D

@rtk-query/codegen-openapi

npm i https://pkg.pr.new/@rtk-query/codegen-openapi@60b02f1 -D
yarn add https://pkg.pr.new/@rtk-query/codegen-openapi@60b02f1.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/codegen-openapi@60b02f1.tgz -D
bun add https://pkg.pr.new/@rtk-query/codegen-openapi@60b02f1.tgz -D

@rtk-query/graphql-request-base-query

npm i https://pkg.pr.new/@rtk-query/graphql-request-base-query@60b02f1 -D
yarn add https://pkg.pr.new/@rtk-query/graphql-request-base-query@60b02f1.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/graphql-request-base-query@60b02f1.tgz -D
bun add https://pkg.pr.new/@rtk-query/graphql-request-base-query@60b02f1.tgz -D

@reduxjs/toolkit

npm i https://pkg.pr.new/@reduxjs/toolkit@60b02f1 -D
yarn add https://pkg.pr.new/@reduxjs/toolkit@60b02f1.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/toolkit@60b02f1.tgz -D
bun add https://pkg.pr.new/@reduxjs/toolkit@60b02f1.tgz -D

commit: 60b02f1

@netlify

netlify Bot commented Aug 27, 2026

Copy link
Copy Markdown

✅ Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 60b02f1
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/6a90b7c91864a50008c61264
😎 Deploy Preview https://deploy-preview-5395--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@aryaemami59 aryaemami59 changed the title fix(toolkit): flag rejectedWithValue for falsy rejectWithValue payloads fix(toolkit): flag rejectedWithValue for falsy rejectWithValue payloads Aug 28, 2026
@markerikson
markerikson merged commit 2e87331 into reduxjs:master Sep 19, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants