-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[typescript] Incompatible types between React.forwardRef and OverridableComponent #32420
Comments
Due to Typescript limitations, wrapping a generic component in a To work around this issue, cast whatever the This should work: import React from "react";
import MUITypography, { TypographyTypeMap, TypographyProps } from "@mui/material/Typography";
import { OverridableComponent } from "@mui/material/OverridableComponent";
export const Typography: OverridableComponent<TypographyTypeMap> = React.forwardRef(function Typography<
D extends React.ElementType
>(props: TypographyProps<D>, ref: React.Ref<HTMLSpanElement> | null) {
return <MUITypography ref={ref} variantMapping={defaultVariantMapping} {...props} />;
}) as OverridableComponent<TypographyTypeMap>; Let me know if this helps. |
Hi @michaldudak thanks for your reply 🙏 In the issue description I've outlined a change to MUI lib's |
Hi @HHK1, sorry for the delayed response. I took a closer look at OverridableComponent and I don't see a reason for not including |
We ran into the same problem in our project and i just found this issue. I seems like there was no PR for this yet, so i took the liberty to open one #35311 |
@michaldudak - Your solution for casting the returned value of the It seems that this PR didn't solve the problem. |
I cannot confirm.
That does not produce a Typescript Error, which is what was fixed by the PR. |
@rotemee Please open a new issue and provide a codesandbox with reproduction steps if you still encounter issues in this area. |
found this issue while trying to figure out how to properly wrap mui components. I am struggling with how to get both
gives me TS issues if the code that uses the component is something like:
|
this is the only way i've gotten it to work thus far: https://mui.com/material-ui/guides/typescript/#complications-with-the-component-prop |
I have also struggled with this while defining wrapper components. This solution also worked 👇 . // Redecalare forwardRef
declare module "react" {
function forwardRef<T, P = {}>(
render: (props: P, ref: React.Ref<T>) => React.ReactNode | null
): (props: P & React.RefAttributes<T>) => React.ReactNode | null;
} Very surprising. |
Duplicates
Latest version
Current behavior 😯
When I try to define a wrapper for a component that uses the OverridableComponent type helper and uses React.forwardRef, I get a typescript error:
Expected behavior 🤔
I would expect types to be compatible
Steps to reproduce 🕹
In a typescript project define this in a file:
Context 🔦
I have an internal package to isolate Mui and either re-export components directly, or wrap them.
If I only export my component without doing an explicit typing with
OverridableComponent
, I don't have a proper autocompletion working, which is why I'd like to use that in the first place.If I update the OverridableComponent to the following (returning
JSX.Element | null
instead of justJSX.Element
)Then the type issue disappears.
Your environment 🌎
`npx @mui/envinfo`
This is not browser related.
Typescript version: 4.5.4
@types/react-dom: 17.0.14
@types/react: 17.0.43
The text was updated successfully, but these errors were encountered: