javascript - 我在哪里从库中获取变量的 typescript 类型

标签 javascript typescript font-awesome typescript-typings react-typescript

我一直在开发一个react-typescript项目,在该项目中我使用了很多外部库,并且我经常发现自己对这些库使用任何类型。请看下面的代码进行演示

这是用于Fontawesome 库

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
  faSquare,
} from '@fortawesome/free-regular-svg-icons';

interface IProps {
  title: string;
  icon: any;
  onClick?: () => void;
  sx?: { [index: string]: string };
}

const icons ={
 title :_,
icon : faSquare
}

对于上面的代码,图标的类型是什么。其值为faSquare。当我将鼠标悬停在其上时,类型为 IconDefinition。但是,我不确定在哪里导入它或者是否可以这样做。

最佳答案

@fortawesome 包嵌入了自己的类型,因此 icons.icon 的类型是自动推断的。

在 VSCode 中,您可以右键单击“转到类型定义”来检查类型。 在您的情况下, faSquare 的类型是 IconDefinition

export interface IconDefinition extends IconLookup {
  icon: [
    number, // width
    number, // height
    string[], // ligatures
    string, // unicode
    IconPathData // svgPathData
  ];
}

您只需将其添加到来自 @fortawesome/free-regular-svg-icons' 的导入

import {faSquare, IconDefinition} from '@fortawesome/free-regular-svg-icons';

关于javascript - 我在哪里从库中获取变量的 typescript 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72109247/

相关文章:

javascript - 我的 Javascript/Html 交通灯序列需要一个循环

angular - 如何从匿名函数访问全局 typescript 变量

TypeScript类型继承: How to assign variable with parent type to the variable with child type

javascript - 在 ios safari 隐私浏览模式下更改位置时保留存储空间

javascript - 文字不出现在按钮上

来自@types 中声明的命名空间的 typescript 扩展接口(interface)

javascript - 谷歌地图 v3 FontAwesome5

html - 使 FA 在 :hover with <li> 上改变颜色

javascript - 如果字体下载被阻止,则从 FontAwesome 回退

javascript - 在Google Charts LineChart中绘制多个DataTable