Skip to content

[Link] typescript error Property 'component' does not exist on type IntrinsicAttributes ... #16846

@linlycode

Description

@linlycode

Link does not accept component property on some cases

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

I've looked #14970 #15827 but didn't find the solution

Expected Behavior 🤔

<Link component={MyComponent} />

should compile with no error

Current Behavior 😯

In some cases(required props not supplied), typescript reports error Property 'component' does not exist on type IntrinsicAttributes & AnchorHTMLAttributes<HTMLAnchorElement> ...

Steps to Reproduce 🕹

Link: codesandbox

  1. Open demo.tsx
  2. Sees 3 compilation errors

Code:

import React, { ReactNode } from "react";
import { makeStyles } from "@material-ui/core/styles";
import Link, { LinkProps } from "@material-ui/core/Link";
import { Link as RouterLink } from "react-navi";

const useStyles = makeStyles(theme => ({
  link: {
    marginRight: theme.spacing(1),
    cursor: "pointer"
  }
}));

interface Props {
  to: string;
  children: ReactNode;
}

function SimpleLink(props: Props) {
  const { to, children, ...restProps } = props;
  return (
    <a {...restProps} href={to}>
      {children}
    </a>
  );
}

function IconLink(props: LinkProps) {
  return (
    <Link {...props} component={RouterLink}>
      !SomeIcon
    </Link>
  );
}

export default function ButtonRouter() {
  const classes = useStyles();
  return (
    <>
      <Link className={classes.link}>Plain</Link>

      <Link className={classes.link} component={SimpleLink}>
        !Simple
      </Link>

      <Link
        className={classes.link}
        to="https://github.com"
        component={SimpleLink}
      >
        Simple
      </Link>

      <Link className={classes.link} component={RouterLink}>
        !Navi Router
      </Link>

      <Link
        className={classes.link}
        href="https://frontarm.com/navi"
        component={RouterLink}
      >
        Navi Router
      </Link>

      <IconLink />
    </>
  );
}

Context 🔦

Wrap Link component to provide custom tooltip, styles etc.

Your Environment 🌎

Tech Version
Material-UI ^4.2.1
React 16.8.6
Browser Chrome
TypeScript 3.5.3

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions