Skip to content

[6.5.4] Typescript "useField" and "Field" default types #946

@jlowcs

Description

@jlowcs

Are you submitting a bug report or a feature request?

It's more of a bug in the sense that the recent typing changes made some types become any which breaks our linting.

image

image

What is the current behavior?

// value is of type 'any'
const { input: { value } } = useField<string>('foo');

<Field<string> name="foo">
	// value is of type 'any'
	{({ input: { value } }) => (
		// ...
	)}
</Field>

What is the expected behavior?

value should be of type string in both cases, unless it is overiden by passing the InputValue type argument (which is not practical, and only works on useField).

Other information

Fix for useField is pretty straightforward. Simply set the default type of InputValue to FieldValue:

export function useField<
  FieldValue = any,
  T extends HTMLElement = HTMLElement,
  InputValue = FieldValue // <--- here
>(
  name: string,
  config?: UseFieldConfig<FieldValue, InputValue>
): FieldRenderProps<FieldValue, T, InputValue>;

It's trickier for field though, because of the order of the parameters.

This doesn't work:

export const Field: <
  FieldValue = any,
  RP extends FieldRenderProps<FieldValue, T, InputValue> = FieldRenderProps<
    FieldValue,
    HTMLElement,
    InputValue // <--- error: used before definition
  >,
  T extends HTMLElement = HTMLElement,
  InputValue = FieldValue
>(
  props: FieldProps<FieldValue, RP, T, InputValue>
) => React.ReactElement;

We would need to reorder the types, but I assume that has some impacts...:

export const Field: <
  FieldValue = any,
  T extends HTMLElement = HTMLElement,
  InputValue = FieldValue,
  RP extends FieldRenderProps<FieldValue, T, InputValue> = FieldRenderProps<
    FieldValue,
    HTMLElement,
    InputValue // <--- 'any' becomes 'InputValue'
  >
>(
  props: FieldProps<FieldValue, RP, T, InputValue>
) => React.ReactElement;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions