From 677c5a9d16d19ae22072e71a113eb99e6d1138c2 Mon Sep 17 00:00:00 2001 From: Corbin Crutchley Date: Thu, 9 Jan 2025 00:19:11 -0800 Subject: [PATCH] chore: add some new type tests --- packages/form-core/src/FieldApi.ts | 39 +++++++++++++-- packages/form-core/src/FormApi.ts | 9 ++-- packages/form-core/tests/FieldApi.test-d.ts | 53 +++++++++++++++++++++ 3 files changed, 94 insertions(+), 7 deletions(-) diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts index 57789636b..c13d9668f 100644 --- a/packages/form-core/src/FieldApi.ts +++ b/packages/form-core/src/FieldApi.ts @@ -731,7 +731,18 @@ export class FieldApi< /** * The field state store. */ - store!: Derived> + store!: Derived< + FieldState< + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn + > + > /** * The current field state. */ @@ -793,7 +804,16 @@ export class FieldApi< return { value, meta, - } as FieldState + } as FieldState< + TData, + TOnMountReturn, + TOnChangeReturn, + TOnChangeAsyncReturn, + TOnBlurReturn, + TOnBlurAsyncReturn, + TOnSubmitReturn, + TOnSubmitAsyncReturn + > }, }) @@ -970,8 +990,19 @@ export class FieldApi< /** * Sets the field metadata. */ - setMeta = (updater: Updater) => - 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. diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index a9754fb76..16f5643dc 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -59,7 +59,9 @@ export type FormValidateFn< TReturnType = unknown, > = (props: { value: TFormData - formApi: FormApi + any + > }) => TReturnType /** @@ -1504,7 +1507,7 @@ export class FormApi< */ setFieldMeta = >( field: TField, - updater: Updater, + updater: Updater>, ) => { this.baseStore.setState((prev) => { return { diff --git a/packages/form-core/tests/FieldApi.test-d.ts b/packages/form-core/tests/FieldApi.test-d.ts index 92ad5cb20..a6271a6dd 100644 --- a/packages/form-core/tests/FieldApi.test-d.ts +++ b/packages/form-core/tests/FieldApi.test-d.ts @@ -146,3 +146,56 @@ it('should type an array sub-field properly', () => { assertType(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) +})