reactjs - 是否可以在 JSX 中指定全局类型?
问题描述
我在项目中使用 Preact,但我已经配置了设置,因此我不必一直这样做import { h } from 'preact'
。然而,问题是,preact.d.ts
如果我不导入它,它就不会被选中,而且我收到的不是 Preact 的 html 属性,而是全局 React 的属性:
带preact
进口:
我知道这似乎无关紧要,但我想访问一些 Preact 的属性,而不是全局 React 的属性。node_modules/preact/index.d.ts
是否可以通过某种配置指向?我没有使用打字稿作为语言或编译自己,只有 VSCode。
解决方案
VSCode 不知道如何使用你的 preact 类型。您应该在项目的根目录(https://code.visualstudio.com/docs/languages/jsconfig)中添加一个 jsconfig.json 文件,在其中指定您的 jsxFactory (这是 vscode 使用的 TS 编译器选项https://www .typescriptlang.org/docs/handbook/compiler-options.html )
{
"compilerOptions": {
"jsx": "react",
"jsxFactory": "h",
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
推荐阅读
- android - 在android studio中创建项目时这个错误是什么我无法理解
- java - 如何固定响应时间以在 Java 中调用 REST API?
- php - 如何将一行连接到数据库中的表?
- java - 确定字符串中字母顺序的代码——返回变量初始化错误
- javascript - 当我只上传一次时,图像在表格中插入了两次
- javascript - 动态附加的文本字段值未通过 javascript 中的 id 打印
- ag-grid - 使用 forEachNode 的行选择非常慢
- python - 将抓取的表存储为字典并输出为 pandas DataFrame
- android - 如何在 Android Studio 中更改默认语言(国际而非编程)
- google-apps-script - 绘制边框脚本的应用不一致