Skip to content

[types] Fix variant props callback type to spread ownerState #46187

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

Merged
merged 4 commits into from
May 29, 2025

Conversation

siriwatknp
Copy link
Member

@siriwatknp siriwatknp commented May 21, 2025

Root cause

The pattern below is logically correct based on the implementation but failed on type check.

const DateTimePickerToolbarTimeContainer = styled('div', {
  name: 'MuiDateTimePickerToolbar',
  slot: 'TimeContainer',
  shouldForwardProp: (prop) => shouldForwardProp(prop) && prop !== 'toolbarVariant',
})<{ ownerState: { pickerOrientation: 'landscape' | 'portrait'; toolbarVariant: PickerVariant }>({
  display: 'flex',
  flexDirection: 'row',
  variants: [
    {
      // ❌ error
      props: ({ pickerOrientation, toolbarVariant }) =>
        pickerOrientation === 'landscape' && toolbarVariant !== 'desktop',
      style: {
        flexDirection: 'column',
      },
    },
  ],
});

This PR fixes the issue above. A test (using MUI X pickers code) is added to ensure that it fixes the issue.

@siriwatknp siriwatknp added typescript v7.x bug 🐛 Something doesn't work labels May 21, 2025
@mui-bot
Copy link

mui-bot commented May 21, 2025

Netlify deploy preview

https://deploy-preview-46187--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 57b2f58

@siriwatknp siriwatknp requested review from LukasTy and DiegoAndai May 21, 2025 09:20
@LukasTy
Copy link
Member

LukasTy commented May 21, 2025

I've tried using the packages from this PR, and here are a couple of issues that are still present:

  1. The inferred prop type is wrapped in Partial, even though in the defined ownerState it is specified as non-nullable.
    I.e. Screenshot 2025-05-21 at 16 20 41 Screenshot 2025-05-21 at 16 20 52
  2. We have a similar effect on https://github.com/mui/mui-x/blob/4da49621a437a98e9e6433dfd3ba3be50f7d252e/docs/data/date-pickers/date-range-calendar/CustomDateRangePickerDay.tsx#L13-L18 where props are defined, but the inferred variants props become type | undefined, which causes TS issues. 🙈

Is there a reason for props being Partial instead of as defined in types? 🤔

@siriwatknp
Copy link
Member Author

siriwatknp commented May 21, 2025

Is there a reason for props being Partial instead of as defined in types? 🤔

Good catch. It should not be a Partial for callback signature. I have removed it in 1301df1.

A test added to ensure it works for the PickerPopperPaper.

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

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

I've tested the changes on mui/mui-x#17802.
Thanks for the improvements! 💙 💯
They helped us uncover some suboptimal ownerState type definitions as well as one actual bug! 🎉 🙏

@LukasTy
Copy link
Member

LukasTy commented May 22, 2025

@siriwatknp siriwatknp merged commit a619144 into mui:master May 29, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work typescript v7.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants