-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[system] component
prop lost when wrapping with styled
#29875
Comments
This could have something to do with how |
component
prop lost when wrapping with styled
component
prop lost when wrapping with styled
@dantman is there an issue for that bug? I'm experiencing an issue when using styled on a 'div' where its children components are unmounting and wondering if it is related to that or I should create my own issue. |
I have a similar issue. After adding the "ref" property to the component, the component loses "component" property. But, the compiler doesn't swear.
Or this is phpStorm problems... Does not offer this option from the list |
I am also having this issue. Any word on when this might be resolved? I don't see an issue corresponding to the overall TypeScript generic loss problem. |
Not sure if this helps narrow down the problem, but it seems that import React from 'react';
import { SvgIcon } from '@mui/material';
type Props = React.ComponentProps<typeof SvgIcon>;
type IsComponentInProps = 'component' extends keyof Props ? true : false; // This should be `true`, but it's `false` |
Can confirm this issue still exists in 5.4.4
|
Related #29191 |
In my case I fixed this by adding the prop
|
import type {
OverridableComponent,
OverridableTypeMap,
OverrideProps,
} from "@mui/types";
import type { PropsOf, Theme } from "@emotion/react";
import type { StyledOptions } from "@emotion/styled";
/** @see https://github.com/mui/material-ui/issues/29875 */
declare module "@emotion/styled" {
export interface CreateStyled {
<
CC extends React.ElementType,
CM extends OverridableTypeMap,
C extends OverridableComponent<CM>
>(
component: C,
options?: StyledOptions<
React.ComponentProps<C> & {
component: CC;
} & OverrideProps<CM, CC>
>
): CreateStyledComponent<
PropsOf<C> & {
theme?: Theme;
} & {
component?: CC;
} & OverrideProps<CM, CC>,
{},
{
ref?: React.Ref<InstanceType<C>>;
}
>;
}
} I solved using module augmentation. you can find CreateStyled declarations, which are the type of /**
* @desc
* This function accepts a React component or tag ('div', 'a' etc).
*
* @example styled(MyComponent)({ width: 100 })
* @example styled(MyComponent)(myComponentProps => ({ width: myComponentProps.width })
* @example styled('div')({ width: 100 })
* @example styled('div')<Props>(props => ({ width: props.width })
*/
export interface CreateStyled {
<
C extends React.ComponentClass<React.ComponentProps<C>>,
ForwardedProps extends keyof React.ComponentProps<C> &
string = keyof React.ComponentProps<C> & string
>(
component: C,
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
): CreateStyledComponent<
Pick<PropsOf<C>, ForwardedProps> & {
theme?: Theme
},
{},
{
ref?: React.Ref<InstanceType<C>>
}
>
<C extends React.ComponentClass<React.ComponentProps<C>>>(
component: C,
options?: StyledOptions<React.ComponentProps<C>>
): CreateStyledComponent<
PropsOf<C> & {
theme?: Theme
},
{},
{
ref?: React.Ref<InstanceType<C>>
}
>
<
C extends React.ComponentType<React.ComponentProps<C>>,
ForwardedProps extends keyof React.ComponentProps<C> &
string = keyof React.ComponentProps<C> & string
>(
component: C,
options: FilteringStyledOptions<React.ComponentProps<C>, ForwardedProps>
): CreateStyledComponent<
Pick<PropsOf<C>, ForwardedProps> & {
theme?: Theme
}
>
<C extends React.ComponentType<React.ComponentProps<C>>>(
component: C,
options?: StyledOptions<React.ComponentProps<C>>
): CreateStyledComponent<
PropsOf<C> & {
theme?: Theme
}
>
<
Tag extends keyof JSX.IntrinsicElements,
ForwardedProps extends keyof JSX.IntrinsicElements[Tag] &
string = keyof JSX.IntrinsicElements[Tag] & string
>(
tag: Tag,
options: FilteringStyledOptions<JSX.IntrinsicElements[Tag], ForwardedProps>
): CreateStyledComponent<
{ theme?: Theme; as?: React.ElementType },
Pick<JSX.IntrinsicElements[Tag], ForwardedProps>
>
<Tag extends keyof JSX.IntrinsicElements>(
tag: Tag,
options?: StyledOptions<JSX.IntrinsicElements[Tag]>
): CreateStyledComponent<
{ theme?: Theme; as?: React.ElementType },
JSX.IntrinsicElements[Tag]
>
}
edit: i realized import type {
OverridableComponent,
OverridableTypeMap,
OverrideProps,
} from "@mui/types";
import type { PropsOf } from "@emotion/react";
import type { StyledOptions } from "@emotion/styled";
/** @see https://github.com/mui/material-ui/issues/29875 */
declare module "@mui/styled-engine" {
export interface CreateMUIStyled<
MUIStyledCommonProps extends {},
MuiStyledOptions,
Theme extends object
> {
<
CC extends React.ElementType,
CM extends OverridableTypeMap,
C extends OverridableComponent<CM>
>(
component: C,
options?: StyledOptions<
PropsOf<C> &
MUIStyledCommonProps & {
component: CC;
} & OverrideProps<CM, CC>
>
): CreateStyledComponent<
PropsOf<C> &
MUIStyledCommonProps & {
component?: CC;
} & OverrideProps<CM, CC>,
{},
{
ref?: React.Ref<InstanceType<C>>;
}
>;
}
} |
Duplicates
Latest version
Current behavior 😯
I discovered this while trying to override
component
for a styledListSubheader
in our app. I tried it forListItem
as well but not others - it seems that when wrapping a component withstyled
, thecomponent
prop is lost and TS errors out.Expected behavior 🤔
component
prop is not lostSteps to reproduce 🕹
See codesandbox
Context 🔦
No response
Your environment 🌎
No response
The text was updated successfully, but these errors were encountered: