简体   繁体   English

如何在 React/Typescript 中使用导入的类型

[英]How to use imported type in React/Typescript

import { IoLogOutOutline } from 'react-icons/io5';    
import { IconType } from 'react-icons';

How do I use the imported type 'Icon Type'?如何使用导入的类型“图标类型”?

(alias) type IconType = (props: IconBaseProps) => JSX.Element

I am able to pass an icon as a prop when setting the type of icon to JSX.Element.将图标类型设置为 JSX.Element 时,我可以将图标作为道具传递。

type LinkProps = {
    to: string;
    icon?: JSX.Element;
};

<Link to="logout" icon={<IoLogOutOutline />} />

However if I set it to 'Icon Type' it returns with the error:但是,如果我将其设置为“图标类型”,则会返回错误:

"Type 'Element' is not assignable to type 'IconType'.
  Type 'ReactElement<any, any>' provides no match for the signature '(props: IconBaseProps): Element'.ts(2322)"

IconType is a function type, that returns some props and expects an element, so try passing a function to the icon prop icon={() => <IoLogOutOutline />} IconType是一个 function 类型,它返回一些道具并期望一个元素,因此请尝试将 function 传递给图标道具icon={() => <IoLogOutOutline />}

import { IoLogOutOutline } from 'react-icons/io5';    
import { IconType } from 'react-icons';

type LinkProps = {
    to: string;
    icon?: IconType;
};

function Link(props: LinkProps) {
    return ...;
}

<Link to="logout" icon={() => <IoLogOutOutline />} />

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

相关问题 如何使用 typescript 返回一个导入数组类型的所有项之和? - How to use typescript to return the sum of all items of an imported array type? 如何在 React 中使用导入的图像? - How to use an imported image in React? 如何使用 TypeScript 编译器 API 对使用 `require()` 导入的模块进行类型检查? - How to use the TypeScript Compiler API to type-check modules imported using `require()`? 如何在 TypeScript 中使用抽象 class 作为 React 组件的类型? - How to use an abstract class as the type of a React component in TypeScript? 如何使用导入数组构建 Typescript 字符串文字类型? - How can I build a Typescript string literal type with an imported array? 如何在 React PropTypes 中使用导入值? - How to use imported values in React PropTypes? 如何使用导入的库作为类型属性 - How to use imported libraries as a type property 在 Typescript/React 中使用一种或另一种接口类型 - Use one or another interface type in Typescript/React 如何在打字稿中使用条件导入模块的类型? - How can I use the types of a conditionally imported module in typescript? 如何使用 Typescript 转换与 React - How to use Typescript Transform with React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM