You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use a controlled form with a Select component but I'm getting a Typescript error on the onValueChange(field.onChange)
Error
Types of parameters 'event' and 'value' are incompatible.
Type 'string' is not assignable to type '"Intermediate" | "Native" | ChangeEvent<Element>'.ts(2322)
index.d.ts(14, 5): The expected type comes from property 'onValueChange' which is declared here on type 'IntrinsicAttributes & SelectProps'
What I've tried
I can fix it with something like that (e: Proficiency) => field.onChange(e) but I want to avoid hard typing it like that. Why is it not infering the type?
Code
// Constantsconstproficiency=["Intermediate","Native"]// The controlled form<FormFieldcontrol={form.control}name="proficiency"render={({ field })=>(<FormItem><FormLabel>Language Proficiency</FormLabel><SelectonValueChange={field.onChange}defaultValue={field.value}><FormControl><SelectTrigger><SelectValueplaceholder="Please select"/></SelectTrigger></FormControl><SelectContent>{proficiency.map((proficiency)=>(<SelectItemkey={proficiency}value={proficiency}>{proficiency}</SelectItem>...// The Validatorja_proficiency: z.enum(["Intermediate","Native"]),
Solution I'm looking for
I want the types to automatically infer on the onChange handler with the select options and zod in combination with useForm().
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem
I'm trying to use a controlled form with a Select component but I'm getting a Typescript error on the
onValueChange(field.onChange)
Error
What I've tried
I can fix it with something like that
(e: Proficiency) => field.onChange(e)
but I want to avoid hard typing it like that. Why is it not infering the type?Code
Solution I'm looking for
I want the types to automatically infer on the onChange handler with the select options and zod in combination with useForm().
Beta Was this translation helpful? Give feedback.
All reactions