form icon indicating copy to clipboard operation
form copied to clipboard

fix(form-core): fix DeepValue from record values being wrong

Open LeCarbonator opened this issue 7 months ago • 7 comments

Closes #1551

This PR fixes a wrong intersection that occurs when iterating over template strings (such as records). The issue arose because of TypeScript inferring the following:

type DeepKeysExample = 'records' | `records.${string}` | `records.${string}.name`

type GetValueFrom = 'records.foo.name'
// This matches both `records.${string}.name` as well as `records.${string}`.

// Generated DeepValue: 
// { name: string } & string

// With PR fix:
// string

[!IMPORTANT] TypeScript has a limitation with template string types. Nested records are not yet safe. Example below:

type NestedRecord = {
  numberRecord: Record<string, number>
  objectRecord: Record<string, { age: number }>
}

type RecordWrapper = {
  records: Record<string, NestedRecord>
}

type Test1 = DeepValue<RecordWrapper, 'records.foo.numberRecord.bar'>
// Type: NestedRecord & number
// `records.${'foo'}.numberRecord.${'bar'}`
// `records.${'foo.numberRecord.bar'}`

type Test2 = DeepValue<RecordWrapper, 'records.foo.objectRecord.bar'>
// Type: NestedRecord & { age: number }
// `records.${'foo'}.objectRecord.${'bar'}`
// `records.${'foo.objectRecord.bar'}`

type Test3 = DeepValue<RecordWrapper, 'records.foo.objectRecord.bar.age'>
// Type: number
// `records.${'foo'}.objectRecord.${'bar'}.age`

LeCarbonator avatar Jun 05 '25 16:06 LeCarbonator

View your CI Pipeline Execution ↗ for commit da26a5ca14eb244851ca333a88f6facc0ac06941

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 16s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 26s View ↗

☁️ Nx Cloud last updated this comment at 2025-11-11 07:55:45 UTC

nx-cloud[bot] avatar Jun 05 '25 16:06 nx-cloud[bot]

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1558
@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1558
@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@1558
@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1558
@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1558
@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@1558
@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1558
@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1558
@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1558

commit: da26a5c

pkg-pr-new[bot] avatar Jun 05 '25 16:06 pkg-pr-new[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 90.53%. Comparing base (6892ed0) to head (da26a5c). :warning: Report is 47 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1558      +/-   ##
==========================================
+ Coverage   90.35%   90.53%   +0.18%     
==========================================
  Files          38       38              
  Lines        1752     1807      +55     
  Branches      444      466      +22     
==========================================
+ Hits         1583     1636      +53     
- Misses        149      151       +2     
  Partials       20       20              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jun 05 '25 16:06 codecov[bot]

thank you for fixing this! is there a timeline on when it will be included in a release? 👀

binajmen avatar Aug 05 '25 16:08 binajmen

@binajmen Well, the main issue is that this isn't really a fix. This PR has been a bit stale because of that ... but I'm not sure there will be a convenient solution that doesn't need reworking all of the DeepKeys types

LeCarbonator avatar Aug 05 '25 18:08 LeCarbonator

I made progress! It now shows possible values as union instead of an intersection. Will commit the changes tomorrow.

LeCarbonator avatar Nov 05 '25 21:11 LeCarbonator

🦋 Changeset detected

Latest commit: da26a5ca14eb244851ca333a88f6facc0ac06941

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/form-core Patch
@tanstack/react-form Patch
@tanstack/angular-form Patch
@tanstack/form-devtools Patch
@tanstack/lit-form Patch
@tanstack/solid-form Patch
@tanstack/svelte-form Patch
@tanstack/vue-form Patch
@tanstack/react-form-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Nov 06 '25 10:11 changeset-bot[bot]