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
}