How to use forwardRef component with MDXProvider and keep TypeScript happy? #2086
-
I'm having a difficult time making TypeScript happy while using a Here is a minimal example that does not type-check with TypeScript. // _app.tsx
const components = {
a: Link,
};
...
<MDXProvider components={components}>
...
</MDXProvider> // Link.tsx
type LinkProps = React.AnchorHTMLAttributes<HTMLAnchorElement>;
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(({ children, ...rest }, ref) => (
<a ref={ref} {...rest}>
{children}
</a>
));
Link.displayName = "Link"; TypeScript throws the following error:
I found 2 workarounds so far but both aren't pretty.
export const LinkWithoutRef: React.FC<React.PropsWithoutRef<Parameters<typeof Link>[0]>> = ({ children, ...rest }) => (
<Link {...rest}>{children}</Link>
); Am I missing some obvious solution here? I'd appreciate expert advice. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
To get types to work, it is important to use the same types on your end as are allowed in the MDX types. |
Beta Was this translation helpful? Give feedback.
-
The workaround doesn't work now? I got this error:
The only way I got it to ignore is to use MDX's own type definition for a function component. |
Beta Was this translation helpful? Give feedback.
To get types to work, it is important to use the same types on your end as are allowed in the MDX types.
The MDX types are here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/mdx/types.d.ts#L22, which expands to
JSX.IntrinsicElements['a']
.