Skip to content

Commit 209a737

Browse files
committed
fix(core): fix formOptions type inferance
1 parent b9480c7 commit 209a737

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

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

Lines changed: 129 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,132 @@ 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+
bool: boolean
155+
}
156+
>
157+
158+
const formOpts = formOptions({
159+
defaultValues: {
160+
firstName: '',
161+
lastName: '',
162+
} as FormData,
163+
listeners: {
164+
onSubmit: ({ formApi, meta }) => {
165+
expectTypeOf(formApi).toEqualTypeOf<FormApiListeners>()
166+
expectTypeOf(meta).toEqualTypeOf<never>()
167+
},
168+
},
169+
})
170+
})
171+
172+
it('types should infer listeners types with submitMeta', () => {
173+
type FormData = {
174+
firstName: string
175+
lastName: string
176+
}
177+
type SubmitMeta = { bool: boolean }
178+
179+
type FormApiListeners = FormApi<
180+
FormData,
181+
FormValidateOrFn<FormData> | undefined,
182+
FormValidateOrFn<FormData> | undefined,
183+
FormAsyncValidateOrFn<FormData> | undefined,
184+
FormValidateOrFn<FormData> | undefined,
185+
FormAsyncValidateOrFn<FormData> | undefined,
186+
FormValidateOrFn<FormData> | undefined,
187+
FormAsyncValidateOrFn<FormData> | undefined,
188+
FormValidateOrFn<FormData> | undefined,
189+
FormAsyncValidateOrFn<FormData> | undefined,
190+
FormAsyncValidateOrFn<FormData> | undefined,
191+
SubmitMeta
192+
>
193+
194+
const formOpts = formOptions({
195+
defaultValues: {
196+
firstName: '',
197+
lastName: '',
198+
} as FormData,
199+
onSubmitMeta: { bool: false } as SubmitMeta,
200+
listeners: {
201+
onSubmit: ({ formApi, meta }) => {
202+
expectTypeOf(formApi).toEqualTypeOf<FormApiListeners>()
203+
expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
204+
},
205+
},
206+
})
207+
})
79208
})

packages/form-core/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"moduleResolution": "Bundler"
4+
"moduleResolution": "Bundler",
5+
"noErrorTruncation": true
56
},
67
"include": ["src", "tests", "eslint.config.js", "vite.config.ts"]
78
}

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)