简体   繁体   English

在对象中键入 svg 元素反应

[英]typing svg element react in an object

I am having issues typing the following.我在输入以下内容时遇到问题。

The issue is with the TeamIcon .问题出在TeamIcon

My object is defined as follows.我的对象定义如下。

import TeamIcon from './components/icons/TeamIcon';

export const teamObject: Record<
  string,
  Record<string, string | React.ReactSVGElement>
> = {
  team: {
    icon: TeamIcon,
    color: '#B2649B',
  }
}

My TeamIcon looks like this:我的TeamIcon看起来像这样:

export default (props: React.SVGProps<SVGSVGElement>) => (
  <svg width="18" height="14" viewBox="0 0 18 18" {...props}>
    <path
      fill="currentColor"
      fillRule="evenodd"
      d="..."
    />
  </svg>
);

Then the following error is being displayed:然后显示以下错误:

JSX element type 'Icon' does not have any construct or call signatures. JSX 元素类型“图标”没有任何构造或调用签名。

const Icon = currentTeam.icon;

<Icon
  width="29px"
  height="29px"
  style={{ color: currentTeam.color }}
/>

Does anyone know how to type this correctly?有谁知道如何正确输入?

TeamIcon isn't a type here, it's the name of local variable, that has value of a React functional component. TeamIcon在这里不是类型,它是局部变量的名称,具有 React 功能组件的值。

So I think you want something like this, which is the type of a React functional component, with props of your choosing.所以我认为你想要这样的东西,它是 React 功能组件的类型,带有你选择的道具。

icon: React.FC<React.SVGProps<SVGSVGElement>>

A full example would be something like:一个完整的例子是这样的:

interface TeamObject {
  team: {
    icon: React.FC<React.SVGProps<SVGSVGElement>>,
    color: string,
  }
}

const teamObject: TeamObject = {
  team: {
    icon: TeamIcon,
    color: "#B2649B"
  }
};

Working example工作示例


You'll note that I got rid of this:你会注意到我摆脱了这个:

Record<
  string,
  Record<string, string | React.ReactSVGElement>
>

I'm not really sure what the goal of that was, but you clearly have two properties with very different types.我不太确定这样做的目标是什么,但是您显然有两个类型非常不同的属性。 You have icon which is a function that returns React.ReactNode and you have color which is a string.你有icon是一个返回React.ReactNode的函数,你有color是一个字符串。 So you should be typing those explicitly.所以你应该明确地输入这些。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM