Skip to content
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

fix(core): add support to union types for DeepValue #724

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
11 changes: 9 additions & 2 deletions packages/form-core/src/util-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// Hack changing Typescript's default get behavior in order to work with unions
type Get<T, K extends string> = T extends { [Key in K]: infer V }
? V
: T extends { [Key in K]?: infer W }
? W | undefined
: never

type Nullable<T> = T | null
type IsNullable<T> = [null] extends [T] ? true : false

Expand Down Expand Up @@ -139,8 +146,8 @@ export type DeepValue<
? DeepValue<DeepValue<TValue, TBefore>, TAfter>
: TAccessor extends string
? TNullable extends true
? Nullable<TValue[TAccessor]>
: TValue[TAccessor]
? Nullable<Get<TValue, TAccessor>>
: Get<TValue, TAccessor>
: never
: // Do not allow `TValue` to be anything else
never
24 changes: 24 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,30 @@ describe('form api', () => {
expect(form.state.errors).toStrictEqual(['first name is required'])
})

it('should read and update union objects', async () => {
const form = new FormApi({
defaultValues: {
person: { firstName: 'firstName' },
} as { person: { firstName: string } | { age: number } },
})

const field = new FieldApi({
form,
name: 'person.firstName',
})
field.mount()
expect(field.getValue()).toStrictEqual('firstName')

form.setFieldValue('person', { age: 0 })

const field2 = new FieldApi({
form,
name: 'person.age',
})
field2.mount()
expect(field2.getValue()).toStrictEqual(0)
})

it('should update a nullable object', async () => {
const form = new FormApi({
defaultValues: {
Expand Down
42 changes: 42 additions & 0 deletions packages/form-core/tests/util-types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,54 @@ type NestedNullableKeys = DeepValue<
>
assertType<'hello' | null>(0 as never as NestedNullableKeys)

type NestedUndefinedKeys = DeepValue<
{
meta: { mainUser: 'hello' } | undefined
},
'meta.mainUser'
>
assertType<'hello'>(0 as never as NestedUndefinedKeys)

type NestedObjectUnionCase = {
normal:
| { a: User }
| { a: string }
| { b: string }
| { c: { user: User } | { user: number } }
}
type NestedObjectUnionA = DeepValue<NestedObjectUnionCase, 'normal.a.age'>
assertType<number>(0 as never as NestedObjectUnionA)
type NestedObjectUnionB = DeepValue<NestedObjectUnionCase, 'normal.b'>
assertType<string>(0 as never as NestedObjectUnionB)
type NestedObjectUnionC = DeepValue<NestedObjectUnionCase, 'normal.c.user.id'>
assertType<string>(0 as never as NestedObjectUnionC)

type NestedNullableObjectUnionCase = {
nullable: { a?: number; b?: { c: boolean } | null }
}
type NestedNullableObjectUnionA = DeepValue<
NestedNullableObjectUnionCase,
'nullable.a'
>
assertType<number | undefined>(0 as never as NestedNullableObjectUnionA)
type NestedNullableObjectUnionB = DeepValue<
NestedNullableObjectUnionCase,
'nullable.b.c'
>
assertType<boolean | null>(0 as never as NestedNullableObjectUnionB)

type NestedArrayExample = DeepValue<{ users: User[] }, 'users[0].age'>
assertType<number>(0 as never as NestedArrayExample)

type NestedLooseArrayExample = DeepValue<{ users: User[] }, 'users[number].age'>
assertType<number>(0 as never as NestedLooseArrayExample)

type NestedArrayUnionExample = DeepValue<
{ users: string | User[] },
'users[0].age'
>
assertType<number>(0 as never as NestedArrayUnionExample)

type NestedTupleExample = DeepValue<
{ topUsers: [User, 0, User] },
'topUsers[0].age'
Expand Down
Loading