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

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

Closed
jlowcs opened this issue Sep 29, 2021 · 1 comment · Fixed by #947
Closed

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

jlowcs opened this issue Sep 29, 2021 · 1 comment · Fixed by #947

Comments

@jlowcs
Copy link

jlowcs commented Sep 29, 2021

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;
@erikras
Copy link
Member

erikras commented Sep 30, 2021

Published fix in v6.5.7.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants