-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add correct types for Field
#3927
Comments
@jaredpalmer I have already done maybe 60% of the work, but i will have to get your approval that this is actually the behavior we want. I am also wondering if you think we should bump the minor or major version for this? The types just assert what has been in the docs already, so there won't be very large migrations that have to be done, but in codebases that use Typescript loosely, we might want to recommend people to cast to We also have to update the documentation, at least for Typescript. I dont have an overview of what has to be changed yet. |
Closing as duplicate of #3099 |
@johnsonav1992 No, they aren't merged. We are moving to react-hook-form now. |
Got it. Yeah, I understand your choice. We are hoping for good things from TanStack Form soon! |
For now, we came up with this workaround (we do happen to have a component with a Adding to this issue, I think the // `Path` from `react-hook-form`
// See https://github.com/react-hook-form/react-hook-form/blob/8838b54acd057a7eecf829510eba07720ccc9ffb/src/types/path/eager.ts#L62
export type FieldProps = { name: Path<Values>, ... } Anyway: import React from "react";
import { Field } from "formik";
import type { FieldProps } from "formik";
// Ideally, we could use the TS utility type `Parameters<Component>[0]` to get
// the props type of a component, but it only works for functions, not classes.
// See https://www.typescriptlang.org/docs/handbook/utility-types.html.
export type ComponentProps<T> = T extends React.ComponentType<infer P> ? P : never;
// Make `FieldProps` props optional, as they are passed in to the custom
// component automatically; The `meta` prop is not passed to a custom component,
// despite the docs not excluding it from the `FieldProps` type. See
// https://formik.org/docs/api/field#component. and
// https://github.com/jaredpalmer/formik/issues/3927,
// https://github.com/jaredpalmer/formik/issues/3091.
// https://github.com/jaredpalmer/formik/blob/v2.1.4/packages/formik/src/Field.tsx#L192
export type FieldSafeProps<T> = Partial<Omit<ComponentProps<T>, "meta">> &
Partial<Omit<FieldProps, "meta">> & {
component: T;
};
export function FieldSafe<T>(props: FieldSafeProps<T>) {
return <Field {...props} />;
} |
@adi518 Yes, this would be great! I have actually done something similar in a form library I am building called Formularity. Check it out here (you can go to the Essentially I pass the |
Feature request
Current / Desired Behavior
Type the Field props properly. Currently the props are
any
, which means we dont get intellisense when we want to add props to the Field, and we also dont get any type errors when for example the name is missing.Currently, these examples typecheck just fine, but are all slightly wrong. They should throw the expected errors as in the
// @ts-expect-error
directiveName should be required:
Type of
field
in children props should beFieldInputProps
:Custom props should be typed:
When a custom component is passed in, the props to Field should not include props from input element. This is to avoid type mismatches in case the custom component has a prop with the same name as an input prop.
Suggested Solution
Remove
& T
fromFieldAttributes
.Add a new generic to
FieldAttributes
andFieldConfig
to allow for custom props to be passed in.Remove
& GenericFieldHTMLAttributes
fromFieldAttributes
conditionally based on the presence ofcomponent
,children
oras
prop.The guiding principles for this implementation are the expected behavior/tests above. This is not quite finished yet, but I think the new types will look something like this:
Here
Who does this impact? Who is this for?
This will impact all users that use TypeScript. I have added these types to our local repository, and among 100 Field usages, there were about 10 different type errors that needed to be fixed. For me, they were pretty easy to fix, as i knew what changes had been done, but they might come as unwanted surprises for others if we change the types in minor release. I think we might have to create a migration guide for this, and maybe bump the major version so people are prepared to do some type-work for this change.
Describe alternatives you've considered
Additional context
Will solve these issues:
#2086 (closed, but has gotten a lot of comments after it was closed)
#3885
#3913
Related pull request that have tried to solve the issue:
#3896
#3774 (reverted by #3775)
The text was updated successfully, but these errors were encountered: