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
below is it's just pseudo code!!
plz just focus on logic
importReactfrom'react';import{useForm,SubmitHandler}from'react-hook-form';import{zodResolver}from'@hookform/resolvers/zod';import*aszfrom'zod';typeFormData={name?: string}constMyForm: React.FC=()=>{const{ register, handleSubmit,formState: { errors }}=useForm<FormData>({defaultValue: {name: undefined}// it's just pseudo code});**// HERE IS THE QUESTION!!**constonSubmit: SubmitHandler<FormData>=(data)=>{**// data type is string | undefined****Q.iwanttonarrowtypelikedata.namewhichisstring(notoptional)?? becauseRHFcontrolleralreadychecksrequiredtypewithrules!!**=>butrecentversion's RHF'sdata.nametype is string|undefined};return(<formonSubmit={handleSubmit(onSubmit)}><div><label>Name</label><input{...register('name')+rules: {required: true}/>{errors.name&&<p>{errors.name.message}</p>}</div><buttontype="submit">Submit</button></form>);};exportdefaultMyForm;
Q. i want to narrow type like data.name which is string(not optional)??
because RHF controller already checks required type with rules!! but recent version's RHF's data.name type is string | undefined
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
below is it's just pseudo code!!
plz just focus on logic
Q. i want to narrow type like data.name which is string(not optional)??
because RHF controller already checks required type with rules!! but recent version's RHF's data.name type is string | undefined
Beta Was this translation helpful? Give feedback.
All reactions