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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use MUI's styleProps pattern to pass variants to styled() in TypeScript projects #27530
Comments
Thanks for the feedback. const DemoRoot = styled("div", {
name: "Demo",
slot: "Root"
})(({ theme, styleProps }) => ({
backgroundColor: "lightgrey",
...(styleProps.variant === "red" && {
backgroundColor: "red"
}),
...(styleProps.variant === "blue" && {
backgroundColor: "blue"
})
})); is already not passing type-checking since a const DemoRoot = styled("div", {
name: "Demo",
slot: "Root"
-})(({ theme, styleProps }) => ({
+})<{ styleProps: {variant: 'blue' | 'red' }}>(({ theme, styleProps }) => ({
backgroundColor: "lightgrey",
...(styleProps.variant === "red" && {
backgroundColor: "red"
}),
...(styleProps.variant === "blue" && {
backgroundColor: "blue"
})
})); Might be interesting to explore if we can automatically augment the types with all the literals returned from Though I fear that TypeScript will default to returning |
The topic described in the present issue seems related to #27234. The basic question here seems to be how the object styleProps could be generated in its form in a dynamic way. Perhaps for the typing of the overridesResolver also the values returned by the function shouldForwardProp is meaningful, in order to compute a flexible type. Thx! |
What exact problem is not solved with #27530 (comment)? You are providing the props per component creation, you can make them loose if you need to. Can you provide more concrete example of what is not working or is difficult to be implemented. |
I think this should be included in the docs instead of making it as a feature. |
Current Behavior 馃槸
When using
styled()
to create styled components we need some way to pass things likevariant
props to the styles.MUI's own internal code handles this by adding a
styleProps
prop and using that in styles. i.e.styleProps={{variant}}
and thenstyleProps.variant === 'foo'
in the style code.However this does not work in projects written in TypeScript.
CreateMUIStyled
gets the props that are permitted from the component/tag you are forwarding props to. As a result the created component does not accept any prop other than the component's own props. So all styled-only props (styleProps, variant, etc...) are type errors.Expected Behavior 馃
There should be some way to pass style props like variant to components made with
styled()
in a type safe way.One possibility could be officially codifying the
styleProps
pattern and adding adefaultStyleProps
to styled's options. Just likeCreateMUIStyled
infers props from the component/tag the type could be written in a way thatstyleProp
's type would be inferred by the type of thedefaultStyleProps
you pass.Steps to Reproduce 馃暪
Your Environment 馃寧
`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: