question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generic type issue with new useForm hook

See original GitHub issue

What package has an issue

@mantine/form

Describe the bug

When passing a type generic to the new @mantine/form useForm hook, the types for the validate object are not inferred correctly. All value types become unknown.

Sandbox link

Screen_Shot_2022-03-14_at_19 39 32

Furthermore, the unknown types are strictly enforced by TypeScript, e.g. it’s not possible to type them in the function parameter because that becomes incompatible with unknown.

There has already been some conversation around this in Discord, but I wanted to document the issue here because in its current state the hook is very hard to use with any types that cannot be inferred from initialValues.

In which browser did the problem occur

N/A

If possible, please include a link to a codesandbox with the reproduced problem

https://codesandbox.io/s/strange-elgamal-vd0ern?file=/src/App.tsx

Do you know how to fix the issue

No

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

I had a look at the types and I’m not sure quite sure why this is happening. I would be happy to help make a PR but I can’t quite figure out what the PR would look like for now 😅 if anyone has a hint as to what to try, I will be happy to give it a shot!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alexturpincommented, Mar 27, 2022

@alexturpin I know this doesn’t answer your question, but I always roll with react-hook-form. This isn’t a knock on Mantine’s hooks. Personally, I haven’t tried them, but rhf is really good at what it does - including inferring types. I’m using it with Mantine on one of my projects right now and it’s worked perfectly without any issue at all.

Thanks for the suggestion! I am indeed aware of other form solutions such as RHF but I like Mantine’s useForm API and I especially like that I can get a simple, low overhead hook for form handling right from Mantine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generic UseForm Hook - reactjs - Stack Overflow
The problem for Typescript is that my starting value of an empty object is not the same shape as T, and so the...
Read more >
TS Support | React Hook Form - Simple React forms validation
Performant, flexible and extensible forms with easy-to-use validation.
Read more >
Generic custom hook “useForm” with “Yup” validation in React
This handler can be passed in input, text area, and select field so we create a type named InputTypes and made it a...
Read more >
React Hook Form: A guide with examples - LogRocket Blog
First, import the useForm Hook from the react-hook-form package: ... syntax is a new implementation to the library that enables strict type ......
Read more >
Form validation with React Hooks WITHOUT a library
This and the following code snippets go into the useForm hook. ... We can add a generic type to any function by adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found