首页 > 解决方案 > 是否可以在 JSX 中指定全局类型?

问题描述

我在项目中使用 Preact,但我已经配置了设置,因此我不必一直这样做import { h } from 'preact'。然而,问题是,preact.d.ts如果我不导入它,它就不会被选中,而且我收到的不是 Preact 的 html 属性,而是全局 React 的属性:

在此处输入图像描述

preact进口:

在此处输入图像描述

我知道这似乎无关紧要,但我想访问一些 Preact 的属性,而不是全局 React 的属性。node_modules/preact/index.d.ts是否可以通过某种配置指向?我没有使用打字稿作为语言或编译自己,只有 VSCode。

标签: reactjstypescriptvisual-studio-codetypescript-typingspreact

解决方案


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/*"
  ]
}

推荐阅读