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

handleChange type doesn't work #3013

Closed
olso opened this issue Jan 25, 2021 · 3 comments
Closed

handleChange type doesn't work #3013

olso opened this issue Jan 25, 2021 · 3 comments

Comments

@olso
Copy link

olso commented Jan 25, 2021

Bug report

Current Behavior

Screenshot 2021-01-25 at 18 33 12

no type error with

onChange(v: number)

Expected behavior

with this commented

Screenshot 2021-01-25 at 18 33 53

we finally get type error

Screenshot 2021-01-25 at 18 34 20

Reproducible example

Suggested solution(s)

https://github.com/formium/formik/blob/80f9a831797429ae3c4457a47b6d8391e9d8bd2c/packages/formik/src/types.tsx#L140-L145

modify preact-like typing

Additional context

Your environment

Software Version(s)
Formik 2.2.6
React 17.0.1
TypeScript 4.0.5
Browser Any
npm/Yarn 1.22.4
Operating System Big Sur
@olso olso added the Type: Bug label Jan 25, 2021
@johnrom
Copy link
Collaborator

johnrom commented Jan 25, 2021

You would have to pass onChange={handleChange("lengthMeterTo")} so it would automatically pass your value back to the field itself.

If that callback is typed with a string, that would be an issue. It should be any. If you need a typescript fix, you can accomplish this right now in a child component of Formik with:

const [{ onChange }] = useField<number>("lengthMeterTo");

// this onChange expects a number.
<YourInput onChange={onChange} />

What you're requesting for handleChange is actually impossible to completely type. Even though you suggest "kind of" typing it, it doesn't type it based off of Values and name, so it is an incomplete type. We're trying to figure out how to give full typing to values and all related callbacks based on name, and have had some success with #1334 #2655 but nothing to merge just yet. Our successful tries require bleeding edge TypeScript, which we're not going to depend on just yet. Probably targeted for v3.

@olso
Copy link
Author

olso commented Jan 25, 2021

Thanks for getting back to me, that solution works for me!

@johnrom
Copy link
Collaborator

johnrom commented Jan 25, 2021

Duplicate of #2084 at al

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants