Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用导入为“`*as”的Typescript定义`_Reactjs_Typescript - Fatal编程技术网

Reactjs 如何使用导入为“`*as”的Typescript定义`

Reactjs 如何使用导入为“`*as”的Typescript定义`,reactjs,typescript,Reactjs,Typescript,我正在使用react图标,并尝试将所有图标定义作为import*导入为“react icons/fi”中的图标 问题是如何创建一个应该是从图标导出的类型之一的类型 e、 g.我正在尝试使道具界面如下: interface Props { icon: Icons // don't know what to do here } 这是react icons/fi/index.d.ts文件的外观: export declare const FiActivity: IconType; export

我正在使用
react图标
,并尝试将所有图标定义作为
import*导入为“react icons/fi”中的图标
问题是如何创建一个应该是从图标导出的类型之一的类型

e、 g.我正在尝试使道具界面如下:

interface Props {
   icon: Icons // don't know what to do here
}
这是
react icons/fi/index.d.ts
文件的外观:

export declare const FiActivity: IconType;
export declare const FiAirplay: IconType;
export declare const FiAlertCircle: IconType;
export declare const FiAlertOctagon: IconType;
export declare const FiAlertTriangle: IconType;
export declare const FiAlignCenter: IconType;
.....

我假设您需要
Icons
名称空间中所有图标常量的名称,因为所有图标的类型似乎都是相同的
IconType

在这种情况下,您可以使用
keyof
typeof
的组合来实现:

import * as Icons from 'react-icons/fi'

interface Props {
   icon: keyof typeof Icons
}
typeof
将获取图标的类型
keyof
然后将该类型的键的名称作为联合类型返回。因此,您得到的类型最终将是
“FiActivity”|“FiAirplay”|“FiAlertCircle”|……

如果您想将这些常量中的任何一个作为一个整体来接受,而不仅仅是名称,那么您可以直接使用
IconType

import * as Icons from 'react-icons/fi';
import {IconType} from 'react-icons';

interface Props {
   icon: IconType
}

我假设您需要
Icons
名称空间中所有图标常量的名称,因为所有图标的类型似乎都是相同的
IconType

在这种情况下,您可以使用
keyof
typeof
的组合来实现:

import * as Icons from 'react-icons/fi'

interface Props {
   icon: keyof typeof Icons
}
typeof
将获取图标的类型
keyof
然后将该类型的键的名称作为联合类型返回。因此,您得到的类型最终将是
“FiActivity”|“FiAirplay”|“FiAlertCircle”|……

如果您想将这些常量中的任何一个作为一个整体来接受,而不仅仅是名称,那么您可以直接使用
IconType

import * as Icons from 'react-icons/fi';
import {IconType} from 'react-icons';

interface Props {
   icon: IconType
}