diff --git a/.changeset/dark-suits-itch.md b/.changeset/dark-suits-itch.md new file mode 100644 index 000000000..28640d294 --- /dev/null +++ b/.changeset/dark-suits-itch.md @@ -0,0 +1,5 @@ +--- +'@tanstack/form-core': patch +--- + +Fix FieldWithValue name type to preserve string typing diff --git a/packages/form-core/src/types.public.ts b/packages/form-core/src/types.public.ts index 0a3f0e913..58a58b16f 100644 --- a/packages/form-core/src/types.public.ts +++ b/packages/form-core/src/types.public.ts @@ -50,7 +50,7 @@ export type OneOrMany = TValue | Array * @typeParam TFieldValue - The value type preserved across field reads and update methods. */ export type FieldWithValue = FieldApi< - any, + string, TFieldValue, any, any, diff --git a/packages/form-core/tests/FieldWithValue.test-d.ts b/packages/form-core/tests/FieldWithValue.test-d.ts new file mode 100644 index 000000000..a66fca9c7 --- /dev/null +++ b/packages/form-core/tests/FieldWithValue.test-d.ts @@ -0,0 +1,27 @@ +import { describe, expectTypeOf, it } from 'vitest' +import type { FieldWithValue } from '../src' + +describe('FieldWithValue', () => { + it('should preserve string typing for field name', () => { + const field = null as unknown as FieldWithValue + + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + + const validName: string = field.name + + // @ts-expect-error Field name must not be a number + const invalidName: number = field.name + + // @ts-expect-error Field name does not have Number methods + field.name.toFixed() + + // @ts-expect-error Boolean field must not accept a string + field.handleChange('not a boolean') + + field.handleChange(true) + + void validName + void invalidName + }) +})