Skip to content

Commit

Permalink
chore: add some new type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Jan 9, 2025
1 parent 9be8c7e commit 677c5a9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
39 changes: 35 additions & 4 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,18 @@ export class FieldApi<
/**
* The field state store.
*/
store!: Derived<FieldState<TData>>
store!: Derived<
FieldState<
TData,
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>
>
/**
* The current field state.
*/
Expand Down Expand Up @@ -793,7 +804,16 @@ export class FieldApi<
return {
value,
meta,
} as FieldState<TData>
} as FieldState<
TData,
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>
},
})

Expand Down Expand Up @@ -970,8 +990,19 @@ export class FieldApi<
/**
* Sets the field metadata.
*/
setMeta = (updater: Updater<FieldMeta>) =>
this.form.setFieldMeta(this.name, updater)
setMeta = (
updater: Updater<
FieldMeta<
TOnMountReturn,
TOnChangeReturn,
TOnChangeAsyncReturn,
TOnBlurReturn,
TOnBlurAsyncReturn,
TOnSubmitReturn,
TOnSubmitAsyncReturn
>
>,
) => this.form.setFieldMeta(this.name, updater)

/**
* Gets the field information object.
Expand Down
9 changes: 6 additions & 3 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export type FormValidateFn<
TReturnType = unknown,
> = (props: {
value: TFormData
formApi: FormApi<TFormData, TFormValidator,
formApi: FormApi<
TFormData,
TFormValidator,
// This is technically an edge-type; which we try to keep non-`any`, but in this case
// It's referring to an inaccessible type from the field validate function inner types, so it's not a big deal
any,
Expand All @@ -68,7 +70,8 @@ export type FormValidateFn<
any,
any,
any,
any>
any
>
}) => TReturnType

/**
Expand Down Expand Up @@ -1504,7 +1507,7 @@ export class FormApi<
*/
setFieldMeta = <TField extends DeepKeys<TFormData>>(
field: TField,
updater: Updater<FieldMeta>,
updater: Updater<FieldMeta<any, any, any, any, any, any, any>>,
) => {
this.baseStore.setState((prev) => {
return {
Expand Down
53 changes: 53 additions & 0 deletions packages/form-core/tests/FieldApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,56 @@ it('should type an array sub-field properly', () => {

assertType<string>(field.state.value)
})

it('should have the correct types returned from form validators', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
validators: {
onChange: () => {
return '123' as const
},
},
} as const)

assertType<'123' | undefined>(form.state.errorMap.onChange)
})

it('should have the correct types returned from form validators even when both onChange and onChangeAsync are present', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
validators: {
onChange: () => {
return '123' as const
},
onChangeAsync: async () => {
return '123' as const
},
},
} as const)

assertType<'123' | undefined>(form.state.errorMap.onChange)
})

it('should have the correct types returned from field validators', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
} as const)

const field = new FieldApi({
form,
name: 'name',
validators: {
onChange: () => {
return '123' as const
},
},
})

assertType<'123' | undefined>(field.state.meta.errorMap.onChange)
})

0 comments on commit 677c5a9

Please sign in to comment.