Skip to content

Compose: Preserve value type in useStateWithHistory - #85090

Merged
manzoorwanijk merged 5 commits into
WordPress:trunkfrom
SainathPoojary:fix/compose-use-merge-refs
Nov 18, 2026
Merged

manzoorwanijk merged 5 commits into
WordPress:trunkfrom
SainathPoojary:fix/compose-use-merge-refs

Conversation

@SainathPoojary

@SainathPoojary SainathPoojary commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

What?

Follow up to #84382.
Tracks #67691.

Addresses the useStateWithHistory type follow-up noted during the @wordpress/compose TypeScript conversion in #84382. Preserves the initial value type and makes isStaged optional in setValue. The useMergeRefs follow-up landed in #85029.

Why?

useStateWithHistory returned value as unknown due to unparameterized useReducer inference, and required isStaged on setValue, even though callers typically omit it.

How?

  • Explicitly passes <T> to undoRedoReducer and initReducer in useReducer.
  • Makes isStaged?: boolean optional in the RECORD action and setValue.
  • Removes value as string and @ts-expect-error from the tests, and adds type-only tests using expectTypeOf.
  • Adds a changelog entry to packages/compose/CHANGELOG.md.

Testing Instructions

  1. Switch to Node 24: nvm use
  2. Run unit tests: npm run test:unit -- packages/compose/src/hooks/use-state-with-history
  3. Run typecheck: npm run typecheck -- packages/compose

Use of AI Tools

Drafted with AI assistance, verified and reviewed by me.

@coderabbitai review

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SainathPoojary <sainathpoojary@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the [Package] Compose /packages/compose label Oct 19, 2026
@SainathPoojary
SainathPoojary marked this pull request as ready for review October 19, 2026 13:32
@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • State history preserves the initial value’s type through updates, undo, and redo, including for numeric and object values.
    • The staging flag is now optional when setting a value. Explicit staging choices continue to control undo and redo history.

useStateWithHistory now preserves its state type through reducer setup and allows callers to omit the staging flag. Tests cover type preservation and staging behavior. The changelog records these updates.

Changes: State history types

Layer / File(s) Summary
Preserve state types and optional staging
packages/compose/src/hooks/use-state-with-history/index.ts, packages/compose/src/hooks/use-state-with-history/test/*, packages/compose/CHANGELOG.md
The reducer and initializer use the state type explicitly. setValue accepts an optional staging flag. Tests cover value and setter types, direct input values, and staged updates with undo and redo. The changelog records the hook updates.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Other

Possibly related issues

  • WordPress/gutenberg#67691 — The issue tracks TypeScript conversion and removal of typing workarounds; this change updates the exported types of a compose hook.

Suggested reviewers: manzoorwanijk

Merge Risk: Minimal · up to b793e

This change improves TypeScript typing for useStateWithHistory with no runtime change, and no merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title names the package (Compose), the hook (useStateWithHistory), and the main change (preserving the value type). It matches the changeset, which explicitly types useReducer with the generic T a…
Description check Passed The description states the purpose: preserving the initial value type and making isStaged optional in setValue. It lists the code changes (generic useReducer parameters, optional isStaged in RECORD an…
Full details: Title check

Explanation

The title names the package (Compose), the hook (useStateWithHistory), and the main change (preserving the value type). It matches the changeset, which explicitly types useReducer with the generic T and fixes the inferred unknown value type.

Full details: Description check

Explanation

The description states the purpose: preserving the initial value type and making isStaged optional in setValue. It lists the code changes (generic useReducer parameters, optional isStaged in RECORD and setValue, test updates with expectTypeOf, and a changelog entry), which match the raw summary. It also gives testing steps.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8d50bbd and b793ee8.

Files selected for processing: 2

  • packages/compose/CHANGELOG.md
  • packages/compose/src/hooks/use-state-with-history/test/index.jsdom.test.tsx
Files skipped from review as they are similar to previous changes: 1

  • packages/compose/CHANGELOG.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment thread packages/compose/CHANGELOG.md Outdated

### Bug Fixes

- `useMergeRefs`, `useStateWithHistory`: Accept `false` in `useMergeRefs`, preserve the initial value type in `useStateWithHistory`, and allow omitting `isStaged` in `setValue` ([#85090](https://github.com/WordPress/gutenberg/pull/85090)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here:

};

describe( 'useStateWithHistory', () => {
it( 'preserves the number value type through undo and redo', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the expectTypeOf assertions are the new coverage here. The act/undo/redo blocks in this test and the next repeat what "should allow undo/redo" already checks, so these two tests could keep only the type assertions.

@manzoorwanijk manzoorwanijk added [Type] Code Quality Issues or PRs that relate to code quality props-bot Manually triggers Props Bot to ensure the list of props is up to date. labels Nov 13, 2026
@github-actions github-actions Bot removed the props-bot Manually triggers Props Bot to ensure the list of props is up to date. label Nov 13, 2026
@SainathPoojary
SainathPoojary force-pushed the fix/compose-use-merge-refs branch from 3b79e06 to 8d50bbd Compare November 17, 2026 15:57
@SainathPoojary SainathPoojary changed the title Compose: Accept false refs in useMergeRefs Compose: Preserve value type in useStateWithHistory Nov 17, 2026
@manzoorwanijk
manzoorwanijk merged commit fb5f0fb into WordPress:trunk Nov 18, 2026
93 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Compose /packages/compose [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants