Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TypeScript to infer form shape from initial state #147

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Allow TypeScript to infer form shape from initial state #147

wants to merge 1 commit into from

Conversation

ahtcx
Copy link

@ahtcx ahtcx commented Mar 17, 2021

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:
image

after:
image

@wsmd
Copy link
Owner

wsmd commented Mar 18, 2021

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.

@wsmd wsmd added the under review The issue is being reviewed by the maintainer label Mar 18, 2021
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.
@ahtcx
Copy link
Author

ahtcx commented Mar 18, 2021

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. 🎉

@ahtcx ahtcx marked this pull request as draft March 18, 2021 20:58
@ahtcx
Copy link
Author

ahtcx commented Mar 18, 2021

Actually have come across as an issue with my typings - have set the PR to a draft whilst I figure it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
under review The issue is being reviewed by the maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants