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

Add explicit undefineds to MinimalInputProps #280

Merged

Conversation

haines
Copy link
Contributor

@haines haines commented May 12, 2023

With the compiler option exactOptionalPropertyTypes is set to true (e.g. when using @tsconfig/strictest base config), the type of getInputProps can't be inferred correctly.

For example, using the Checkbox component from the docs app:

Checkbox.tsx:21:49 - error TS2345: Argument of type '{ type: string; value: string | undefined; }' is not assignable to parameter of type 'Omit<MinimalInputProps, HandledProps | Callbacks> & Partial<Pick<MinimalInputProps, Callbacks>>'.
  Object literal may only specify known properties, and 'value' does not exist in type 'Omit<MinimalInputProps, HandledProps | Callbacks> & Partial<Pick<MinimalInputProps, Callbacks>>'.

21           {...getInputProps({ type: "checkbox", value })}
                                                   ~~~~~


Found 1 error in Checkbox.tsx:21

The generic type argument for getInputProps is inferred as MinimalInputProps, because the actual props of the input element don't extend that type. Explicitly providing the type argument makes that clear:

Checkbox.tsx:21:29 - error TS2344: Type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>' does not satisfy the constraint 'MinimalInputProps'.
  Types of property 'onChange' are incompatible.
    Type 'ChangeEventHandler<HTMLInputElement> | undefined' is not assignable to type '(...args: any[]) => void'.
      Type 'undefined' is not assignable to type '(...args: any[]) => void'.

21           {...getInputProps<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>>({ type: "checkbox", value })}
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in Checkbox.tsx:21

Adding an explicit | undefined to all the prop types in MinimalInputProps (to match the definition in @types/react) fixes the issue.

Signed-off-by: Andrew Haines <andrew@haines.org.nz>
@vercel
Copy link

vercel bot commented May 12, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
remix-validated-form ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 12, 2023 0:42am

Copy link
Owner

@airjp73 airjp73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@airjp73 airjp73 merged commit 1da61dd into airjp73:main Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants