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

Better support for exactOptionalPropertyTypes: true #3388

Open
jakubmazanec opened this issue Jul 16, 2024 · 0 comments
Open

Better support for exactOptionalPropertyTypes: true #3388

jakubmazanec opened this issue Jul 16, 2024 · 0 comments

Comments

@jakubmazanec
Copy link

Describe the bug and the expected behavior

When TypeScript compiler option exactOptionalPropertyTypes is set to true, you cannot pass undefined as a value to an object property that is marked as optional but without explicit undefined type. While IMO very useful option, it can lead to bad DX, because it can cause TypeScript errors:

import {Checkbox} from '@headlessui/react';

type CustomComponentProps = {
  name?: string | undefined;
}

let CustomComponent = ({name}: CustomComponentProps ) => {
 return <Checkbox name={name} />; // error here, because `name` prop can be `undefined`
}

I therefore prefer that "input" types (like function parameters, properties in options objects, etc.) have explicit undefined added to them; e.g. here should look like this:

export type CheckboxProps<
  TTag extends ElementType = typeof DEFAULT_CHECKBOX_TAG,
  TType = string,
> = Props<
  TTag,
  CheckboxRenderPropArg,
  CheckboxPropsWeControl,
  {
    value?: TType | undefined
    disabled?: boolean | undefined
    indeterminate?: boolean | undefined

    checked?: boolean | undefined
    defaultChecked?: boolean | undefined
    autoFocus?: boolean | undefined
    form?: string | undefined
    name?: string | undefined
    onChange?: ((checked: boolean) => void) | undefined
  }
>

Would it be possible to add undefined to all input types? Thank you.

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

No branches or pull requests

1 participant