react-use-form-state
react-use-form-state copied to clipboard
Allow TypeScript to infer form shape from initial state
Remove unneeded signature for useFormState
and makes the generic signature form shape default to StateShape<any>
.
This allows the TypeScript compiler to infer the form shape form the initial values, which allows for strongly typed forms, without having to create a new interface and pass it in the generic signature.
before:
after:
Thanks for your contribution, @ahtcx!
The change has caused a number of regressions: https://travis-ci.org/github/wsmd/react-use-form-state/builds/763322010#L221
You can test the types changes locally using this file: https://github.com/wsmd/react-use-form-state/blob/master/test/types.tsx
Ideally this patch should be backward compatible if possible so we don't introduce a breaking change.
Hi @wsmd, sorry I didn't see the checks until now. I just pushed a fix for those tests, which allows for generic and non-generic input initializers.
This means that the input initializers are not fully strongly-typed (they allow names that aren't in the state keys, which is required for backwards compatibility) but we still get useful intellisense options in VS Code.
I think it would be nice if they were removed in the next breaking release to get a fully inferred strongly-typed form, but have no strong opinions on it.
Thanks for the great library BTW, only heard of it recently and it's looking like it'll be fully replacing Formik for our use-cases. 🎉
Actually have come across as an issue with my typings - have set the PR to a draft whilst I figure it out