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

Allow validationOptions?: ValidationOptions in FormContextValues['register'] #1963

Closed
JoshuaKGoldberg opened this issue Jun 25, 2020 · 4 comments
Labels
TS Typescript related issues

Comments

@JoshuaKGoldberg
Copy link

JoshuaKGoldberg commented Jun 25, 2020

Is your feature request related to a problem? Please describe.
I'd like to be able to call register with a validationOptions parameter of type ValidationOptions | undefined.

import React from 'react';

const exampleInput = (validationOptions?: ValidationOptions) => {
  return <input ref={register(validationOptions)} />;
}

However, in TypeScript's strict type checking mode, this gives an unhappy complaint:

src/index.tsx:27:21 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'Partial<{ required: string | boolean | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ValidationValueMessage<...>; ... 5 more ...; validate: Validate | Record<...>; }> | undefined' is not assignable to parameter of type 'Partial<{ required: string | boolean | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ValidationValueMessage<...>; ... 5 more ...; validate: Validate | Record<...>; }> | ... 4 more ... | null'.
      Type 'undefined' is not assignable to type 'Partial<{ required: string | boolean | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ValidationValueMessage<...>; ... 5 more ...; validate: Validate | Record<...>; }> | ... 4 more ... | null'.

4  return <input ref={register(validationOptions)} />;
                               ~~~~~~~~~~~~~~~~~

  ../../node_modules/react-hook-form/dist/contextTypes.d.ts:12:5
    12     register<Element extends FieldElement = FieldElement>(refOrValidationOptions: ValidationOptions | Element | null, validationOptions?: ValidationOptions): ((ref: Element | null) => void) | void;
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.

This is because register's first two overloads say that you can either have no validationOptions or a defined validationOptions:

export declare type FormContextValues<FormValues extends FieldValues = FieldValues> = {
    register<Element extends FieldElement = FieldElement>(): (ref: Element | null) => void;
    register<Element extends FieldElement = FieldElement>(validationOptions: ValidationOptions): (ref: Element | null) => void;

I.e. you may not have an undefined validationOptions.

Describe the solution you'd like
Could we make that second overload allow ?: ValidationOptions?

-    register<Element extends FieldElement = FieldElement>(validationOptions: ValidationOptions): (ref: Element | null) => void;
+    register<Element extends FieldElement = FieldElement>(validationOptions?: ValidationOptions): (ref: Element | null) => void;

Describe alternatives you've considered
In my code, I could do something like validationOptions ? register(validationOptions) : register(), but that's a little less clean than calling directly.

Additional context
You can see a real life code example in https://github.com/Codecademy/client-modules/pull/852

@bluebill1049
Copy link
Member

we need more details on this one, can you supply a codesandbox?

@bluebill1049 bluebill1049 added the status: need more detail Please follow our issue template. label Jun 25, 2020
@bluebill1049
Copy link
Member

@kotarella1110 looks like our register type is mismatching with useForm and useFormContext

@kotarella1110
Copy link
Member

@bluebill1049 I think we just need to fix the overload as follows:

register<
Element extends FieldElement<FormValues> = FieldElement<FormValues>
>(): (ref: Element | null) => void;
register<Element extends FieldElement<FormValues> = FieldElement<FormValues>>(
validationOptions: ValidationOptions,
): (ref: Element | null) => void;

-  register<
-    Element extends FieldElement<FormValues> = FieldElement<FormValues>
-  >(): (ref: Element | null) => void;
-  register<Element extends FieldElement<FormValues> = FieldElement<FormValues>>(
-    validationOptions: ValidationOptions,
-  ): (ref: Element | null) => void;
+  register<Element extends FieldElement<FormValues> = FieldElement<FormValues>>(
+    validationOptions?: ValidationOptions,
+  ): (ref: Element | null) => void;

Do we need to apply this fix to both V5 and V6?

@JoshuaKGoldberg
Copy link
Author

Hmm, I can't get Codesandbox to surface strict null checking errors from TypeScript for some reason, but here's a link with the code that should surface complaints: https://codesandbox.io/s/react-hook-form-useform-template-j0zen?file=/src/index.tsxa

@bluebill1049 bluebill1049 removed the status: need more detail Please follow our issue template. label Jan 24, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
TS Typescript related issues
Projects
None yet
Development

No branches or pull requests

3 participants