-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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
[styles] Return simpler type from ComponentCreator #20854
Conversation
Details of bundle changes.Comparing: 0cd5819...9bae8f2 Details of page changes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but I'm unable to parse what you're doing here. Could you use code that you would use in your codebase?
Unfortunately I have mistaken. It still not working. // Infering props from another component to correct type checking.
// Use `Omit` just for make type objectable to avoid `Rest types may only be created from object types.ts(2700)` error
// in `ComponentWrapper`.
// If you remove Omit or something else many things will not work with TS.
type ComponentWrapperProps<C extends React.ElementType = any> = Omit<
{ component?: C } & (undefined extends C ? unknown : React.ComponentProps<C>),
never
>;
// Component that accept another component in order to do some manipulations with that component.
// Same technique uses in MUI AFAIK
function ComponentWrapper<C extends React.ElementType = 'div'>({
component = 'div' as C,
...rest
}: ComponentWrapperProps<C>): JSX.Element {
console.log(rest);
// some logic
return React.createElement(component);
}
// Using with correct inferred props
<ComponentWrapper component={StyledInferedPropsMyComponent} defaulted="def" />; But in this case we get error:
Why |
The problem is I think it might be best to switch from |
Let's try. I have changed it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the test now. It's more confusing than it helps.
What you can do to prevent regression is add a comment to StyledComponent
that this is the simplest possible component type and that we don't want a union here to support mapped types.
Done. |
@vlazh Perfect, thanks! |
When I use
styled
component like this:I get TS error:
because TS can't assign
FunctionComponent
toComponentClass
and vise versa.So in that situation we need concreate stable component type. I have look at the source of
styled
and found usingforwardRef
.