Skip to content

Comments

fix(types): handle branded types correctly in DeepPartial#13222

Merged
bluebill1049 merged 1 commit intoreact-hook-form:masterfrom
a28689604:fix/branded-types-deep-partial
Dec 28, 2025
Merged

fix(types): handle branded types correctly in DeepPartial#13222
bluebill1049 merged 1 commit intoreact-hook-form:masterfrom
a28689604:fix/branded-types-deep-partial

Conversation

@a28689604
Copy link
Contributor

@a28689604 a28689604 commented Dec 28, 2025

Proposed Changes

DeepPartial and DeepPartialSkipArrayKey incorrectly expanded branded types into object structures instead of treating them as atomic values.

The Problem

type BrandedType<T, Brand extends symbol | string> = T & {
  [K in Brand]: unknown;
};

type FormValues = {
  firstName: BrandedType<string, "FirstName">;
};

Before:
DeepPartial expanded branded strings into all string methods

{
  firstName?: {
    toString?: {} | undefined;
    charAt?: {} | undefined;
    concat?: {} | undefined;
    // ... 35+ more methods ...
    FirstName?: unknown;
  } | undefined
}

After:
Correctly preserves the branded type

{
  firstName?: BrandedType<string, "FirstName">;
}

Solution

Added IsPrimitiveLike<T> helper to detect primitive types (including branded primitives) and prevent DeepPartial/DeepPartialSkipArrayKey from recursively expanding them.

Fixes #13221

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

Branded types (e.g., string & { __brand: 'X' }) are now preserved as-is
instead of being expanded into object structures.

Signed-off-by: a28689604 <[email protected]>
@a28689604 a28689604 force-pushed the fix/branded-types-deep-partial branch from d3a0450 to 3c9fe7e Compare December 28, 2025 17:39
Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

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

Thanks for the fix.

@bluebill1049 bluebill1049 merged commit 76db177 into react-hook-form:master Dec 28, 2025
6 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.

issue: Branded Type in FormValues is incorrectly expanded in defaultValues

2 participants