TanStackT
TanStack6mo ago
5 replies
ordinary-sapphire

Type of error object from useStore() using Zod

I would like to know how to tell TypeScript the type of the error object extracted from useStrore() using zod validation schema.

// FormPage

const validationSchema = z.object({
  name: z.string().min(1, 'Name required'),
});

const formOpts = formOptions({
  defaultValues: {
    name: obj.name || '',
  },
  validators: {
    onChange: validationSchema,
  },
});

const form = useAppForm({...});



//TextField

const field = useFieldContext<string>();
  
const errors = useStore(field.store, (state) => state.meta.errors);

// the real type of errors is :
//     {
//       expected: string,
//       code: string,
//       path: string[],
//       message: string
//     }[]
//
//  it's actually the issues property in ZodError



How can I make typescript know that this is the real type ? Is there a way to get it from Zod ?
Was this page helpful?