Skip to content

Commit 3a6bdc9

Browse files
authored
TypeScript fix accessing error keys of optional nested object (#2718)
* Update types.ts * Update types.ts
1 parent 11930ca commit 3a6bdc9

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

packages/core/src/types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export type FormDataKeys<T> = T extends Function | FormDataConvertibleValue
6464
? never
6565
: T extends unknown[]
6666
? ArrayFormDataKeys<T>
67-
: T extends Record<string, unknown>
67+
: T extends object
6868
? ObjectFormDataKeys<T>
6969
: never
7070

@@ -92,7 +92,7 @@ type ArrayFormDataKeys<T extends unknown[]> = number extends T['length']
9292
/**
9393
* Helper type for object form data keys
9494
*/
95-
type ObjectFormDataKeys<T extends Record<string, unknown>> = string extends keyof T
95+
type ObjectFormDataKeys<T extends object> = string extends keyof T
9696
? string
9797
:
9898
| Extract<keyof T, string>
@@ -105,7 +105,11 @@ type ObjectFormDataKeys<T extends Record<string, unknown>> = string extends keyo
105105
? `${Key}.${FormDataKeys<T[Key]> & string}`
106106
: T[Key] extends Record<string, any>
107107
? `${Key}.${FormDataKeys<T[Key]> & string}`
108-
: never
108+
: Exclude<T[Key], null | undefined> extends any[]
109+
? never
110+
: Exclude<T[Key], null | undefined> extends Record<string, any>
111+
? `${Key}.${FormDataKeys<Exclude<T[Key], null | undefined>> & string}`
112+
: never
109113
}[Extract<keyof T, string>]
110114

111115
type PartialFormDataErrors<T> = {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This component is used for checking the TypeScript implementation; there is no Playwright test depending on it.
2+
import { useForm } from '@inertiajs/react'
3+
4+
interface FormData {
5+
foo: null | {
6+
bar: string
7+
}
8+
}
9+
10+
export default function NullableNestedObject() {
11+
const form = useForm<FormData>({
12+
foo: null,
13+
})
14+
15+
console.log(form.errors['foo.bar'])
16+
17+
return <div></div>
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script lang="ts">
2+
// This component is used for checking the TypeScript implementation; there is no Playwright test depending on it.
3+
import { useForm } from '@inertiajs/svelte'
4+
5+
interface FormData {
6+
foo: null | {
7+
bar: string
8+
}
9+
}
10+
11+
const form = useForm<FormData>({
12+
foo: null,
13+
})
14+
15+
console.log($form.errors['foo.bar'])
16+
</script>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script setup lang="ts">
2+
// This component is used for checking the TypeScript implementation; there is no Playwright test depending on it.
3+
import { useForm } from '@inertiajs/vue3'
4+
5+
interface FormData {
6+
foo: null | {
7+
bar: string
8+
}
9+
}
10+
11+
const form = useForm<FormData>({
12+
foo: null,
13+
})
14+
15+
console.log(form.errors['foo.bar'])
16+
</script>

0 commit comments

Comments
 (0)