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

[core] Use boolean type for ListItem prop instead of 'false' #23338

Closed
wants to merge 4 commits into from

Conversation

ricosandyca
Copy link

@ricosandyca ricosandyca commented Oct 31, 2020

Sometimes, we might need to assign button type as boolean. Last time i tried conditional variable passed to button prop. Something like this

<ListItem button={!!item.onClick} />

But i got an error type caused type boolean isn't assignable to type true or false

For #14971

@ricosandyca ricosandyca changed the title Use boolean type for ListItem prop instead of 'false' [core] Use boolean type for ListItem prop instead of 'false' Oct 31, 2020
@mui-pr-bot
Copy link

mui-pr-bot commented Oct 31, 2020

No bundle size changes

Generated by 🚫 dangerJS against 09c897f

@oliviertassinari oliviertassinari added the duplicate This issue or pull request already exists label Oct 31, 2020
Fix checking error
@ricosandyca ricosandyca reopened this Nov 2, 2020
@eps1lon
Copy link
Member

eps1lon commented Nov 2, 2020

This can be considered better than before but <ListItem button={someBooleanVal} /> will never accept button props. It'll always be considered a li and I'm not sure what's better: failing loudly with "boolean really just doesn't work" or failing silently and complaining later on unassignable props. We're basically kicking type errors down the stack.

@ricosandyca
Copy link
Author

ricosandyca commented Nov 2, 2020

@eps1lon you could still accept the button props if you assign the value of button directly as true

<ListItem
  button
  onClick={e => ....} // `e` variable has `HTMLDivElement` type
/>
<ListItem
  button={boolValue} // or `false`
  onClick={e => ....} // `e` variable has `HTMLLIElement` type
/>

That's why i changed the type order of true value of button as the first checking

@eps1lon
Copy link
Member

eps1lon commented Nov 2, 2020

<ListItem
button={boolValue} // or false
onClick={e => ....} // e variable has HTMLLIElement type
/>

What if boolValue is true at runtime? The typings expect that boolean is the same as false which is not sound. I'm not fully convined that your approach is better. It just seems to hide issues.

@ricosandyca
Copy link
Author

But not always the same. The reasons why ListItem read as div component, because it receive true exact value. Otherwise, if ListItem receive boolean variable which is it could be true or false it will be read as the default component that is li.

So, if you want get some div component types suggestion, you have to assign the button prop as true directly.

I think this is better, cz it can assign the button variable dynamically. Unlike the old one that can only receive static value

@eps1lon
Copy link
Member

eps1lon commented Nov 2, 2020

I think this is better, cz it can assign the button variable dynamically. Unlike the old one that can only receive static value

You're trading soundness. Whether that is "better" depends on how sound you want your types to be. You could achieve the same right now by passing <ListItem button={boolValue as false} />. With that you're directly aware of potential problems. With your change you ain't.

@oliviertassinari
Copy link
Member

For context, in #13597, we discuss breaking down the ListItem component into smaller components. It frees us from this problem.

<nav>
  <List>
    <ListItem>
      <ListItemButton component={NavLink}>
        Foo
      </ListItemButton>
      <ListItemSecondaryAction>
        Bar
      </ListItemSecondaryAction>
    </ListItem>
  </List>
</nav>

@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 2, 2020

@ricosandyca Would it solve #14971? If it does, trading soundness could make sense for an issue with 35 upvotes. Maybe we should add a test case that illustrates the fix?

@ricosandyca
Copy link
Author

@oliviertassinari i think it would, and for the test case i think it would be great

@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 5, 2020

💯 for accepting the changes if:

@eps1lon What do you think?

@oliviertassinari oliviertassinari added component: list This is the name of the generic UI component, not the React module! typescript and removed duplicate This issue or pull request already exists labels Nov 5, 2020
@eps1lon
Copy link
Member

eps1lon commented Nov 5, 2020

It fixes #14971

It doesn't fix it. It just silences the error which can already be achieved by passing <ListItem button={someBoolean as false} />.

I already know that people don't want to do it because of "type casting is bad" but type casting isn't bad inherently. It's only bad if it leads to unsound types and is effectively equivalent to this change proposed here.

But maybe I'm wrong but we can only check that if we have a type test for using HTMLListElement props on <ListItem button={someBoolean} />.

@oliviertassinari
Copy link
Member

Should we close the pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: list This is the name of the generic UI component, not the React module! typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants