Skip to content

🐞 fix(cloneObject): keep the length of sparse arrays - #13834

Merged
bluebill1049 merged 2 commits into
react-hook-form:masterfrom
charan-rathore:fix/clone-sparse-array-length
Oct 7, 2026
Merged

bluebill1049 merged 2 commits into
react-hook-form:masterfrom
charan-rathore:fix/clone-sparse-array-length

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

cloneObject clones an array by starting from [] and copying own enumerable keys in a for...in loop. A sparse array such as new Array(3) has no own keys, so the non-enumerable length never transfers and the clone collapses to []. Sparse arrays passed through defaultValues or reset lose their length on the way back out of getValues.

The fix allocates the copy with new Array(data.length) so the length and holes survive, while the existing own-key loop still deep-copies the assigned slots.

Added a regression spec covering a fully sparse array, trailing holes next to assigned values, and sparse arrays round-tripping through defaultValues and reset.

Tests: the full Jest suite passes on this branch (129 suites, 1516 tests). tsc, eslint and prettier are clean.

Prepared with AI assistance; verified locally as described above.

@bluebill1049
bluebill1049 merged commit 08ddcd6 into react-hook-form:master Oct 7, 2026
6 checks passed
@charan-rathore
charan-rathore deleted the fix/clone-sparse-array-length branch October 7, 2026 23:37
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