Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'string' is not assignable to type '(url: string) => string'.ts(2322)

I have this material ui copyright component:

export default function Copyright(link: string, text: string) {
  return (
    <Typography variant="body2" color="textSecondary" align="center">
      {'Copyright © '}
      <Link color="inherit" href={link}>
        {text}
      </Link>{' '}
      {new Date().getFullYear()}
      {'.'}
    </Typography>
  );
}

If I try to use it like this, I don't get any errors:

{Copyright('https://hello.com', 'HELLO')}

However, if I try to use it like this:

<Copyright link={'https://hello.com'} text={'hello'}></Copyright>

I get this error on link even though I have not specified any 'url' anywhere else:

Type 'string' is not assignable to type '(url: string) => string'.ts(2322)

How can I use this component with the second method? A similar question suggested to use casting but in my case, there can be multiple links with which I want to call the component in the future.


1 Answers

If you want to use

<Copyright link={"https://hello.com"} text={"hello"} />

You need to define the props type like this

props: { link: string; text: string }

Usage:

function Copyright(props: { link: string; text: string }) {
  return (
    <Typography variant="body2" color="textSecondary" align="center">
      {"Copyright © "}
      <Link color="inherit" href={props.link}>
        {props.text}
      </Link>{" "}
      {new Date().getFullYear()}
      {"."}
    </Typography>
  );
}

Edit stupefied-jackson-r8tst

like image 179
keikai Avatar answered Feb 09 '26 10:02

keikai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!