form icon indicating copy to clipboard operation
form copied to clipboard

Field errors returned from onSubmitAsync aren't TS-checked

Open vojtech-dobes opened this issue 7 months ago • 1 comments

Describe the bug

Form's onSubmitAsync validator allows returning object like this:

return {
    fields: {
        field1: `Field1 has a problem`
    }
}

Considering the strong type checks, I expected that if field1 didn't exist, Typscript would scream about this.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMBi0RwL5wBmUEuA5AAIwCGAdqjQMYDWA9FOtYzALSE5kA3AChhjCPXj8ouALwoM2GQAoEwuBrgATdIWrIANjABq1A8nSoAXInWb71GwCInAGjv2NAIxsAGd-Z4AZoAbmbAWtQw0Na2npoSAMrIXiDAMACCqACetIw21Dl5cKpwYebo+ACUcLIAfHHxnhwwyFC0jU1NhMDoBlqxal3DmgAeNjBQFsEjgTPDeCJdQR747nhVIsLSIAB0AOboMJi9-QCStPzKZNRkm9s4B0cnfVoXV2Sjd1tAA

Steps to reproduce

The linked playground already contains the imho incorrect behavior.

Expected behavior

I expected Typescript error about the fields object containing unknown key.

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

This is about Typescript, not code running in browser.

TanStack Form adapter

react-form

TanStack Form version

1.12.0

TypeScript version

5.8.3

Additional context

No response

vojtech-dobes avatar May 29 '25 19:05 vojtech-dobes

Validators can return any error of any structure, which is why TypeScript won't complain. The special case is an object of the structure you provided, which will be mapped to fields. However, I can see that type safety for deep keys would be convenient.

How about a helper function that would give you type safety when creating the object?

LeCarbonator avatar Jul 08 '25 05:07 LeCarbonator