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

Refine Field Component Typing #3896

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-walls-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': minor
---

Enhanced the typing within the `Field` component, rectifying a prior issue that had resulted in all props being implicitly assigned the type of `any`.
18 changes: 8 additions & 10 deletions packages/formik/src/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ export interface FieldConfig<V = any> {
*/
component?:
| string
| React.ComponentType<FieldProps<V>>
| React.ComponentType
| React.ComponentType<FieldProps<V> & { className?: string }>
| React.ComponentType<any>
| React.ForwardRefExoticComponent<any>;

/**
* Component to render. Can either be a string e.g. 'select', 'input', or 'textarea', or a component.
*/
as?:
| React.ComponentType<FieldProps<V>['field']>
| React.ComponentType<FieldInputProps<V> & { className?: string }>
| string
| React.ComponentType
| React.ForwardRefExoticComponent<any>;

/**
Expand Down Expand Up @@ -73,8 +72,7 @@ export interface FieldConfig<V = any> {
}

export type FieldAttributes<T> = { className?: string; } & GenericFieldHTMLAttributes &
FieldConfig<T> &
T & {
FieldConfig<T> & {
name: string,
};

Expand Down Expand Up @@ -134,7 +132,7 @@ export function useField<Val = any>(
return [getFieldProps(props), getFieldMeta(fieldName), fieldHelpers];
}

export function Field({
export function Field<T = any>({
validate,
name,
render,
Expand All @@ -143,7 +141,7 @@ export function Field({
component,
className,
...props
}: FieldAttributes<any>) {
}: FieldAttributes<T>) {
const {
validate: _validate,
validationSchema: _validationSchema,
Expand Down Expand Up @@ -187,8 +185,8 @@ export function Field({
unregisterField(name);
};
}, [registerField, unregisterField, name, validate]);
const field = formik.getFieldProps({ name, ...props });
const meta = formik.getFieldMeta(name);
const field = formik.getFieldProps<T>({ name, ...props });
const meta = formik.getFieldMeta<T>(name);
const legacyBag = { field, form: formik };

if (render) {
Expand Down