我在哪里从库中获取变量的打字稿类型

发布于 2025-01-26 05:09:21 字数 597 浏览 3 评论 0原文

我一直在研究一个反应型项目,在该项目中,我正在使用很多外部库,并且经常发现自己使用为这些库中使用任何类型。请查看下面的代码进行演示,

这是针对上述代码的 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
}

Icon的类型是什么类型。其价值为 fasquare 。当我徘徊时,类型为 icondefinition 。但是,我不确定要在哪里导入它,或者是这样做的方法。

I have been working on a react-typescript project in which I am using a lot of external libraries and I frequently find myself using any type for those libraries. Please look at the code below for the demonstration

This is for the Fontawesome library

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
}

For the above code, what would be the type of icon. The value of which is faSquare. When I hovered over it the type is IconDefinition. But, I am not sure where to import it or if that is the way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2025-02-02 05:09:21

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

在VSCODE中,您可以右键单击并“转到类型定义”以检查类型。
在您的情况下,fasquare的类型是icondefinition

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

您只需将其从@fortawesome/fortawesome/free-free-regular-Svg-icons'

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

The @fortawesome packages embed their own types, thus the type of icons.icon is inferred automatically.

In VSCode, you can right-click and "Go to type definition" to check type.
In your case, the type of faSquare is IconDefinition

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

You can just add it to your import from @fortawesome/free-regular-svg-icons'

import {faSquare, IconDefinition} from '@fortawesome/free-regular-svg-icons';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文