I am new to react and I am using react-hook-form
useFormContext
and it is generating the following error, Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.
and I am not sure why. My code is the following,
const mapFields = new Map([['d1', '1'], ['d1', '2'], ['d3', '3']]); register(mapFields.get('d1'), { required: true })
I tried passing a function into register instead of a map and it worked fine so not sure why this is occurring.
>Solution :
You can use the non-null assertion operator (!
) to tell TypeScript that the value cannot be undefined.
register(mapFields.get('d1')!, { required: true })