Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions packages/form-core/src/util-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,18 @@ export type DeepKeysAndValuesImpl<
? DeepKeyAndValueObject<TParent, T, TAcc>
: TAcc

export type DeepRecord<T> = {
[TRecord in DeepKeysAndValues<T> as TRecord['key']]: TRecord['value']
type DeepRecordWithDynamicSuffix<T> = {
// DeepKeys uses a dot as reserved character, so the only way that it can be a suffix
// is if the suffix is a dynamic variable.
[TRecord in DeepKeysAndValues<T> as `${TRecord['key']}.` extends TRecord['key']
? TRecord['key']
: never]: TRecord['value']
}

type DeepRecordWithStaticSuffix<T> = {
[TRecord in DeepKeysAndValues<T> as `${TRecord['key']}.` extends TRecord['key']
? never
: TRecord['key']]: TRecord['value']
}

/**
Expand All @@ -161,19 +171,22 @@ export type DeepKeys<T> = unknown extends T
? string
: DeepKeysAndValues<T>['key']

/**
* Infer the type of a deeply nested property within an object or an array.
*/
export type DeepValue<TValue, TAccessor> = unknown extends TValue
? TValue
: TAccessor extends DeepKeys<TValue>
? DeepRecord<TValue>[TAccessor]
: never

/**
* The keys of an object or array, deeply nested and only with a value of TValue
*/
export type DeepKeysOfType<TData, TValue> = Extract<
DeepKeysAndValues<TData>,
AnyDeepKeyAndValue<string, TValue>
>['key']

type PickValue<T, K extends keyof T> = T[K]
/**
* Infer the type of a deeply nested property within an object or an array.
*/
export type DeepValue<TValue, TAccessor> = unknown extends TValue
? TValue
: TAccessor extends keyof DeepRecordWithStaticSuffix<TValue>
? PickValue<DeepRecordWithStaticSuffix<TValue>, TAccessor>
: TAccessor extends keyof DeepRecordWithDynamicSuffix<TValue>
? PickValue<DeepRecordWithDynamicSuffix<TValue>, TAccessor>
: never
36 changes: 36 additions & 0 deletions packages/form-core/tests/util-types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,39 @@ type AnyObjectExample4 = DeepValue<ObjectWithAny, 'obj.c'>
expectTypeOf(0 as never as AnyObjectExample4).toEqualTypeOf<any>()
type AnyObjectExample5 = DeepValue<ObjectWithAny, 'obj.d'>
expectTypeOf(0 as never as AnyObjectExample5).toEqualTypeOf<number>()

type RecordExample = {
records: Record<string, { a: string; b: number; c: { d: string } }>
}

expectTypeOf<DeepKeys<RecordExample>>().toEqualTypeOf<
| 'records'
| `records.${string}`
| `records.${string}.a`
| `records.${string}.b`
| `records.${string}.c`
| `records.${string}.c.d`
>()
expectTypeOf<DeepKeysOfType<RecordExample, string>>().toEqualTypeOf<
`records.${string}.a` | `records.${string}.c.d`
>()
expectTypeOf<DeepValue<RecordExample, 'records'>>().toEqualTypeOf<
Record<string, { a: string; b: number; c: { d: string } }>
>()
expectTypeOf<DeepValue<RecordExample, 'records.something'>>().toEqualTypeOf<{
a: string
b: number
c: { d: string }
}>()
expectTypeOf<
DeepValue<RecordExample, 'records.something.a'>
>().toEqualTypeOf<string>()
expectTypeOf<
DeepValue<RecordExample, 'records.something.b'>
>().toEqualTypeOf<number>()
expectTypeOf<DeepValue<RecordExample, 'records.something.c'>>().toEqualTypeOf<{
d: string
}>()
expectTypeOf<
DeepValue<RecordExample, 'records.something.c.d'>
>().toEqualTypeOf<string>()