Skip to content

[Menu] Only require onCheckedChange to handle boolean values #1778

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

Conversation

cpmsmith
Copy link
Contributor

@cpmsmith cpmsmith commented Nov 10, 2022

Description

This makes using CheckboxItem with TypeScript a bit simpler, as it doesn't require state managed up the tree to allow indeterminate states if it doesn't want to. More practically, it makes this allowed:

const [checked, setChecked] = useState(false);
<CheckboxItem
  checked={checked}
  onCheckedChange={setChecked}
/>

Previously that would result in a type error, because setChecked didn't permit indeterminate states. However, CheckboxItem never actually sets itself into an indeterminate state—that can only be set via props—meaning we shouldn't have to support that case if we don't want to.

I've marked this as a patch version because, despite changing a type definition, function types are contravariant in TypeScript, i.e. any function which is assignable to (value: boolean | "indeterminate") => void is also assignable to (value: boolean) => void, so I don't think this should break any existing usage.

You can see the current state of things in this CodeSandbox, as well as the workaround required if you only want your state to be boolean.

This makes using CheckboxItem with TypeScript a bit simpler, as it
doesn't require state managed up the tree to allow indeterminate states
if it doesn't want to. More practically, it makes this allowed:

    const [checked, setChecked] = useState(false);
    <CheckboxItem
      checked={checked}
      onCheckedChange={setChecked}
    />

Previously that would result in a type error, because `checked` didn't
permit indeterminate states. However, CheckboxItem never actually sets
itself into an indeterminate state -- that can only be set via props --
meaning we shouldn't have to support that case if we don't want to.
@cpmsmith cpmsmith changed the title Only require onCheckedChange to handle boolean values [Menu] Only require onCheckedChange to handle boolean values Nov 10, 2022
@benoitgrelard
Copy link
Contributor

Hey @cpmsmith, thanks for your contribution.

However, CheckboxItem never actually sets itself into an indeterminate state—that can only be set via props—meaning we shouldn't have to support that case if we don't want to.

You are absolutely right! In fact this means the types are currently sort of lying, because onCheckedChange can never call you with "indeterminate". Well spotted!

I've marked this as a patch version because, despite changing a type definition, function types are contravariant in TypeScript, i.e. any function which is assignable to (value: boolean | "indeterminate") => void is also assignable to (value: boolean) => void, so I don't think this should break any existing usage.

Yep, that makes sense.

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