Skip to content

Commit

Permalink
fix: maintain the form types when transforming with useTransform hook (
Browse files Browse the repository at this point in the history
  • Loading branch information
Balastrong authored Aug 9, 2024
1 parent 354f63a commit b998045
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/react-form/src/useTransform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { FormApi, Validator } from '@tanstack/form-core'
import type { FormApi, FormTransform, Validator } from '@tanstack/form-core'

export function useTransform<
TFormData,
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
>(
fn: (formBase: FormApi<any, any>) => FormApi<TFormData, TFormValidator>,
deps: unknown[],
) {
): FormTransform<TFormData, TFormValidator> {
return {
fn,
deps,
Expand Down
30 changes: 30 additions & 0 deletions packages/react-form/tests/useTransform.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assertType, it } from 'vitest'
import { formOptions, mergeForm, useForm, useTransform } from '../src'
import { type ServerFormState } from '../src/nextjs/types'

it('should maintain the type of the form', () => {
const state = {} as ServerFormState<any>

const formOpts = formOptions({
defaultValues: {
firstName: '',
age: 123,
} as const,
})

function Comp() {
const form = useForm({
...formOpts,
transform: useTransform(
(baseForm) => mergeForm(baseForm, state),
[state],
),
})

assertType<123>(form.state.values.age)
assertType<string>(form.state.values.firstName)

// @ts-expect-error this should be a number
form.state.values.age = 'age'
}
})

0 comments on commit b998045

Please sign in to comment.