score:8
register()
in react-hook-form
v7 does not accept a string but string literal. The literal value is one of the field name in the form:
interface IFormValues {
firstName: string;
lastName: string;
}
const { register, handleSubmit } = useForm<IFormValues>();
When you assign a generic type parameter to the useForm
hook, the register
expects to receive either firstName
or lastName
literal, a string or anything else will throw a type error.
Solution
According to the official example, if you are using useFieldArray
and the fields are created dynamically using map
, you need to assert the name as a const before passing to the register
, this is because of type widening when you mix string literal with the index number:
<input key={field.id} {...register(`test.${index}.test` as const)} />
Source: stackoverflow.com
Related Query
- Argument of type 'string | null' is not assignable to parameter of type 'ValueFn<SVGPathElement, Datum[], string | number | boolean | null>'
- ReactJS Typescript Argument of type '{ keyPrefix: string; }' is not assignable to parameter of type string
- How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react?
- How to fix error argument of type undefined is not assignable to parameter of type 'string or () => string usng typescript and react?
- React Typescript - Argument of type is not assignable to parameter of type
- Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element'. Type 'null' is not assignable to type 'Element'.ts(2345)
- Argument of type 'unknown' is not assignable to parameter of type '{}'
- Argument of type 'Element' is not assignable to parameter of type 'ReactElement<any>
- react usestate hooks error: argument of type 'yyy' is not assignable to parameter of type 'setstateaction<xx>'
- getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)'
- Argument of type '"MY_EVENTS_LOAD"' is not assignable to parameter of type 'TakeableChannel<unknown>' in yeild takeLatest
- React TypeScript: Argument is not assignable to parameter of type 'never'
- Argument of type 'string' is not assignable to parameter of type '`${string}` | `${string}.${string}` | `${string}.${number}`'
- Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
- Argument of type partial is not assignable to parameter of type
- Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction<Date>'
- React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
- Argument of type 'number' is not assignable to parameter of type 'never' in array.includes()
- Argument of type '() => () => Promise<void>' is not assignable to parameter of type 'EffectCallback'
- Getting an error Argument of type 'unknown' is not assignable to parameter of type 'Error | null'
- Redux Toolkit - Argument of type 'AsyncThunkAction<>' is not assignable to parameter of type 'AnyAction'
- React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'
- Argument of type 'Element[]' is not assignable to parameter of type 'Element'
- Redux - createStore. Argument of type is not assignable to parameter of type 'DeepPartial<any>'
- Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'
- TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement'
- Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
- Argument of type 'typeof test' is not assignable to parameter of type 'Component<{ children?: ReactNode; } & DispatchProp<any>>'
More Query from same tag
- In "@headlessui/react", how to pass 'Popover' 'open' prop to it's parent component
- Trying to reflect changes in Redux State from CurrentUser on React DOM for CurrentUser?
- React + TypeScript: Scroll event type not assignable to window.addEventListener
- Cannot read property of undefined, How to avoid the crash?
- Can you use array.push in useEffect? React Hooks
- In React, how to create a bubble head on top of the thumb of a range slider
- React parent component update when child component alters database
- Access state of parent component in a child component?
- React - Uncaught Error: Element type is invalid
- Don't loadOptions on initial render
- How can we use the CDN URL mechanism for package management if we use Typescript?
- React Bootstrap Forms: <Form.Control.Feedback></Form.Control.Feedback> is not displaying when validated is false
- React change file without rebuild and restart
- Component is not rendering on changing the select
- Getting input value in Redux
- Type error on vsc but still can be complied?
- Redirect to profile after login
- Integrate react-intl with react-table
- React OpenLayers parent context
- Can not upload image to server fetching PHP from react
- Using anchor tags in react-router 4
- Select the current element with the jquery in the react
- How do I log some string on server and give it back to component as props using Next.js?
- How to inject `apollo client` through react context via `contextType` instead of consumer?
- create-react-app not currently working
- Updating state property in componentDidMount not reflecting with react-table
- Connecting React frontend and Node.js backend at Heroku
- Passing back-end data after authentication to be rendered to the screen in React JS?
- Material UI component reference does not work
- Make React JSX build faster with no production