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

[Box] Error when using component property in a styled Box component after upgrading to 5.14.3 #38274

Closed
2 tasks done
doberkofler opened this issue Aug 2, 2023 · 11 comments · Fixed by #38356
Closed
2 tasks done
Assignees
Labels
bug 🐛 Something doesn't work package: system Specific to @mui/system regression A bug, but worse typescript

Comments

@doberkofler
Copy link

doberkofler commented Aug 2, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Link to live example:
https://codesandbox.io/s/old-sky-jvvldp?file=/Demo.tsx:266-275

Steps:
The use of the component property reports an error

Current behavior 😯

After upgrading to 5.14.3 TypeScript reports the following type error:

Type '{ children: Element; component: string; }' is not assignable to type 'IntrinsicAttributes & SystemProps<Theme> & { children?: ReactNode; ref?: Ref<unknown> | undefined; sx?: SxProps<Theme> | undefined; } & Omit<...> & MUIStyledCommonProps<...>'.
  Property 'component' does not exist on type 'IntrinsicAttributes & SystemProps<Theme> & { children?: ReactNode; ref?: Ref<unknown> | undefined; sx?: SxProps<Theme> | undefined; } & Omit<...> & MUIStyledCommonProps<...>'.

Expected behavior 🤔

No type error as with 5.14.1

Context 🔦

No response

Your environment 🌎

npx @mui/envinfo
System:
    OS: macOS 13.5
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    Yarn: Not Found
    npm: 9.8.1 - ~/.nvm/versions/node/v18.16.1/bin/npm
  Browsers:
    Chrome: 115.0.5790.114
    Edge: Not Found
    Safari: 16.6
  npmPackages:
    @emotion/react: 11.10. => 11.11.1
    @emotion/styled: 11.11.0 => 11.11.0
    @mui/base:  5.0.0-beta.9
    @mui/core-downloads-tracker:  5.14.3
    @mui/icons-material: 5.14.3 => 5.14.3
    @mui/material: 5.14.3 => 5.14.3
    @mui/private-theming:  5.13.7
    @mui/styled-engine:  5.13.2
    @mui/system:  5.14.3
    @mui/types:  7.2.4
    @mui/utils:  5.14.3
    @mui/x-data-grid:  6.10.2
    @mui/x-data-grid-pro: 6.10.2 => 6.10.2
    @mui/x-license-pro:  6.10.2
    @types/react: 18.2.18 => 18.2.18
    react: 18.2.0 => 18.2.0
    react-dom: 18.2.0 => 18.2.0
    typescript: 5.1.6 => 5.1.6
@doberkofler doberkofler added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 2, 2023
@sai6855 sai6855 added typescript package: system Specific to @mui/system and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 2, 2023
@michaldudak michaldudak changed the title Error when using component property in a styled Box component after upgrading to 5.14.3 [Box] Error when using component property in a styled Box component after upgrading to 5.14.3 Aug 2, 2023
@michaldudak michaldudak added bug 🐛 Something doesn't work regression A bug, but worse labels Aug 2, 2023
@michaldudak
Copy link
Member

michaldudak commented Aug 2, 2023

Thanks for the report. This behavior is unfortunate but consistent with all the other components in Material UI. We'll try to find a fix for all the components (likely within the styled utility). For now, you can work around this issue by casting the result of styled to typeof Box:

const StyledBox = styled(Box)(() => ({
  fontSize: "100px",
  color: "white"
})) as typeof Box;

cc @brijeshb42, @mnajdova

@doberkofler
Copy link
Author

Thank you for your feedback. As this seems to be a breaking change, should it not be part a a major release and not be hidden in a patch release?

@Reflejo
Copy link

Reflejo commented Aug 4, 2023

@michaldudak Using wildcard BoxPropss + component used to work and now it doesn't, see: https://codesandbox.io/s/angry-cori-dhytrg?file=/Demo.tsx

@michaldudak
Copy link
Member

@doberkofler We were unaware that this use case would break, so it was not done intentionally.

If we don't find the solution until the next release (early next week), we'll revert the PR that caused the problem.

@doberkofler
Copy link
Author

@michaldudak I understand. Thank you for caring!

@kirillku
Copy link

kirillku commented Aug 7, 2023

What should be the workaround if i also pass some extra props to styled component?

export const StyledBox = styled(Box)<{ size?: number }>`
  width: ${(p) => p.size}px;
  height: ${(p) => p.size}px;
` as typeof ???;

UPD
Had to do this

export const StyledBox = styled(Box)<{ size?: number }>`
  width: ${(p) => p.size}px;
  height: ${(p) => p.size}px;
` as React.FC<BoxProps & { size?: number; component?: React.ElementType }>;

@michaldudak
Copy link
Member

michaldudak commented Aug 7, 2023

We are going to revert the commit the caused these breaking changes. Expect a release with the fix today or tomorrow.

@ggascoigne
Copy link

The exact same thing is still breaking with MenuItem using @mui/material@5.14.4, do you want a separate bug or is it still a variation of this one?

https://codesandbox.io/s/great-wildflower-pr8hdy?file=/Demo.tsx

@michaldudak
Copy link
Member

@ggascoigne, this is a different issue. It wasn't caused by the same recent changes, but it was not supported in Material UI v5 (and possibly before). See #29875

@ggascoigne
Copy link

OK thank you. That said, it's worked well until 5.14.4 and it seems weird to say that it's not supported when the underlying JavaScript clearly supports it, it's useful, and only the types are wrong. Either way, I can work around it.

@enricoros
Copy link

enricoros commented Jan 24, 2024

For now, you can work around this issue by casting the result of styled to typeof Box:

...
})) as typeof Box;

Thanks for saving my day @michaldudak . Works with Joy UI as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: system Specific to @mui/system regression A bug, but worse typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants