Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React hook form useFormContext generating "argument of type 'string | undefined'" error

I am new to react and I am using react-hook-form useFormContextand 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the non-null assertion operator (!) to tell TypeScript that the value cannot be undefined.

register(mapFields.get('d1')!, { required: true })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading