Skip to content

Commit f24b44b

Browse files
committed
fix(core): fix formOptions type inferance
1 parent 9e092e2 commit f24b44b

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

packages/form-core/tests/formOptions.test-d.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expectTypeOf, it } from 'vitest'
22
import { FormApi, formOptions } from '../src/index'
3+
import type { FormAsyncValidateOrFn, FormValidateOrFn } from '../src/index'
34

45
describe('formOptions', () => {
56
it('types should be properly inferred', () => {
@@ -76,4 +77,129 @@ describe('formOptions', () => {
7677

7778
expectTypeOf(form.state.values).toExtend<PersonWithAge>()
7879
})
80+
81+
it('types should infer submitMeta', () => {
82+
type FormData = {
83+
firstName: string
84+
lastName: string
85+
}
86+
type SubmitMeta = { bool: boolean }
87+
88+
const formOpts = formOptions({
89+
defaultValues: {
90+
firstName: '',
91+
lastName: '',
92+
} as FormData,
93+
onSubmitMeta: { bool: false } as SubmitMeta,
94+
onSubmit: ({ meta }) => {
95+
expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
96+
},
97+
})
98+
})
99+
100+
it('types should infer validator types', () => {
101+
type FormData = {
102+
firstName: string
103+
lastName: string
104+
}
105+
106+
type formApiValidators = FormApi<
107+
FormData,
108+
any,
109+
any,
110+
any,
111+
any,
112+
any,
113+
any,
114+
any,
115+
any,
116+
any,
117+
any,
118+
any
119+
>
120+
121+
const formOpts = formOptions({
122+
defaultValues: {
123+
firstName: '',
124+
lastName: '',
125+
} as FormData,
126+
validators: {
127+
onChange: ({ value, formApi }) => {
128+
expectTypeOf(value).toEqualTypeOf<FormData>()
129+
expectTypeOf(formApi).toEqualTypeOf<formApiValidators>()
130+
},
131+
},
132+
})
133+
})
134+
135+
it('types should infer listeners types', () => {
136+
type FormData = {
137+
firstName: string
138+
lastName: string
139+
}
140+
141+
type FormApiListeners = FormApi<
142+
FormData,
143+
FormValidateOrFn<FormData> | undefined,
144+
FormValidateOrFn<FormData> | undefined,
145+
FormAsyncValidateOrFn<FormData> | undefined,
146+
FormValidateOrFn<FormData> | undefined,
147+
FormAsyncValidateOrFn<FormData> | undefined,
148+
FormValidateOrFn<FormData> | undefined,
149+
FormAsyncValidateOrFn<FormData> | undefined,
150+
FormValidateOrFn<FormData> | undefined,
151+
FormAsyncValidateOrFn<FormData> | undefined,
152+
FormAsyncValidateOrFn<FormData> | undefined
153+
>
154+
155+
const formOpts = formOptions({
156+
defaultValues: {
157+
firstName: '',
158+
lastName: '',
159+
} as FormData,
160+
listeners: {
161+
onSubmit: ({ formApi, meta }) => {
162+
expectTypeOf(formApi).toEqualTypeOf<FormApiListeners>()
163+
expectTypeOf(meta).toEqualTypeOf<never>()
164+
},
165+
},
166+
})
167+
})
168+
169+
it('types should infer listeners types with submitMeta', () => {
170+
type FormData = {
171+
firstName: string
172+
lastName: string
173+
}
174+
type SubmitMeta = { bool: boolean }
175+
176+
type FormApiListeners = FormApi<
177+
FormData,
178+
FormValidateOrFn<FormData> | undefined,
179+
FormValidateOrFn<FormData> | undefined,
180+
FormAsyncValidateOrFn<FormData> | undefined,
181+
FormValidateOrFn<FormData> | undefined,
182+
FormAsyncValidateOrFn<FormData> | undefined,
183+
FormValidateOrFn<FormData> | undefined,
184+
FormAsyncValidateOrFn<FormData> | undefined,
185+
FormValidateOrFn<FormData> | undefined,
186+
FormAsyncValidateOrFn<FormData> | undefined,
187+
FormAsyncValidateOrFn<FormData> | undefined,
188+
SubmitMeta
189+
>
190+
191+
const formOpts = formOptions({
192+
defaultValues: {
193+
firstName: '',
194+
lastName: '',
195+
} as FormData,
196+
onSubmitMeta: { bool: false } as SubmitMeta,
197+
listeners: {
198+
onSubmit: ({ formApi, meta }) => {
199+
expectTypeOf(formApi).toEqualTypeOf<FormApiListeners>()
200+
expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
201+
},
202+
},
203+
})
204+
})
79205
})

pnpm-workspace.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ packages:
66
- 'examples/vue/**'
77
- 'examples/lit/**'
88
- 'examples/svelte/**'
9+
catalog:
10+
'@tanstack/store': ^0.7.2
11+
arktype: ^2.1.20
12+
valibot: ^1.1.0
13+
zod: ^3.25.76

0 commit comments

Comments
 (0)