Skip to content
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

Props type for overriden components #213

Open
kwiat1990 opened this issue Jun 4, 2023 · 0 comments
Open

Props type for overriden components #213

kwiat1990 opened this issue Jun 4, 2023 · 0 comments

Comments

@kwiat1990
Copy link

Hi, I'm wondering if there's some typing I could use when overriding HTML elements like this:

const components = {
  img: ({
    src,
    ...attrs
    }: React.DetailedHTMLProps<
      React.ImgHTMLAttributes<HTMLImageElement>,
      HTMLImageElement
    >) => {
      return (
        <Image
          dprVariants={[2, 3]}
          placeholder="blur"
          responsive={[
	  {
            size: {
              width: 400,
            }
            maxWidth: 500,
	  }
        ]}
        src={src}
        {...attrs}
    />
  );
}}

export function MDXComponent({ component }: Props) {
  const Component = React.useMemo(
    () => getMDXComponent(component),
    [component],
  );
  return <Component components={components} />;
}

With the types it doesn't work because Image component have placeholder prop, which make Typescript complains about: Type 'string' is not assignable to type '"blur" | "empty" | undefined'.ts(2322) types.d.ts(16, 5): The expected type comes from property 'placeholder' which is declared here on type 'IntrinsicAttributes & ImageProps'. Similar problem comes up when using<div> inside an <li> element and so one. Is there some way to make Typescript happy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant