简体   繁体   English

类型 'string' 不可分配给类型 '(url: string) => string'.ts(2322)

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

I have this material ui copyright component:我有这个材料 ui 版权组件:

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:即使我没有在其他任何地方指定任何“url”,我仍然在链接上收到此错误:

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.一个类似的问题建议使用强制转换,但在我的情况下,可能有多个链接,我想在将来调用该组件。

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>
  );
}

编辑 stupefied-jackson-r8tst

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“元素”不可分配给类型“字符串”.ts(2322) - Type 'Element' is not assignable to type 'string'.ts(2322) 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) 类型 &#39;string&#39; 不能分配给类型 &#39;{ 模型:{ 节点:[]; 链接:[]; }; }&#39;.ts(2322) - Type 'string' is not assignable to type '{ model: { nodes: []; links: []; }; }'.ts(2322) 键入'字符串 | 号码 | boolean' 不能分配给类型 'undefined'。 类型“字符串”不可分配给类型“未定义”.ts(2322) - Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322) angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string TypeScript - TS2322:类型“string”不可分配给类型“str1”| “str2”&#39; - TypeScript - TS2322: Type 'string' is not assignable to type '“str1” | “str2”' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' - React TypeScript Error : TS2322 - Type '' is not assignable to type 'IntrinsicAttributes & String' 动态工厂方法-按字符串实例化类收到错误TS2322:类型“ Util”不能分配给类型“ void” - Dynamic Factory Method - Instantiate Class by String gets receives error TS2322: Type 'Util' is not assignable to type 'void' 类型“未知”不可分配给类型“从不”.ts(2322) - Type 'unknown' is not assignable to type 'never'.ts(2322) 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM