我已经四处寻找解决这个问题的办法了。他们都建议在你的tsconfig中添加"jsx": "react"。json文件。我已经做到了。另一个是添加“include:[]”,我也这样做过。然而,当我试图编辑.tsxfiles时,我仍然得到错误。下面是我的tsconfig文件。

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "allowJs": true,
        "checkJs": false,
        "jsx": "react",
        "outDir": "./build",
        "rootDir": "./lib",
        "removeComments": true,
        "noEmit": true,
        "pretty": true,
        "skipLibCheck": true,
        "strict": true,
        "moduleResolution": "node",
        "esModuleInterop": true
    },
    "include": [
        "./lib/**/*"
    ],
    "exclude": [
        "node_modules"
    ]
}

任何建议都会很有帮助。我使用babel 7编译所有的代码与env, react和typescript预设。如果你们需要更多的文件来帮助调试,请告诉我。


当前回答

这个答案是关于VS Code和这个特定的错误在该IDE中持续存在。(有人可能会觉得这很有用)

如果你正在使用Webpack或其他类似的工具,即使你的tsconfig已经将jsx的编译器选项设置为React,你仍然可能会得到这个错误。

有一个解决方案。问题是VS Code已经内置了自动检测tsc。

为了消除编辑器中的错误,你可以把这个放在你的用户设置中:

{
    "typescript.tsc.autoDetect": "off"
}

请注意,您将不再得到tsc自动检测,但如果您主要使用Webpack等工具或自己处理带有标记的命令,那么这并不是什么大问题。

注意:只有当错误在VS Code中持续存在时才这样做。要确保这种行为持续存在,在配置tsconfig之后重新加载窗口并重新启动编辑器。json文件。

其他回答

我的情况下,没有一个解决方案是有效的,所以我看了ECMA Scrypt版本。我的版本是ec5,你可以在这里检查-> tsconfig.json。所以我尝试将目标:“es5”切换为目标:“es2017”,它似乎可以处理这个问题,如果再次出现任何问题,我会编辑这条评论

在我的例子中,问题是VSCode生成了一个空的tsconfig。Json文件,当然,它什么也没做。

我把这个添加到tsconfig。json from Typescript的React Page:

{
    "compilerOptions": {
        "outDir": "./dist/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react"
    }
}

...然后点击保存,VSCode从那里取出它。

编辑: 自从我最初发布这个答案以来,通用的tsconfig文件已经变得更广泛地可用。如果你想了解这些选项,我已经发布了一个更详细的版本作为主旨。

我必须把我的整个应用程序添加到include中。

我的tsconfig。Json是这样的:

{
    "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "jsx": "react",
    "allowJs": true
  },
  "include": ["./"]
}

崇高文本的解决方案:

Make sure your package.json uses Typescript >= v4.1.2 Uninstall the Sublime Text "Typescript" package through Package Control At time of writing, it bundles Typescript v3.x Open a terminal in the Sublime Text "Packages" directory. cd "~/Library/Application Support/Sublime Text 3/Packages" Clone the Typescript-Sublime-Plugin repo into your Packages directory: git clone https://github.com/microsoft/TypeScript-Sublime-Plugin TypeScript Open User Settings: Sublime Text > Preferences > Settings Add "typescript_tsdk": "node_modules/typescript/lib" This tells the Sublime Typescript plugin to use the project version of Typescript rather than its bundled version. Restart Sublime Text Unlike most Sublime Text settings, this one requires a restart in order to restart the Typescript server.

参考: https://github.com/microsoft/TypeScript-Sublime-Plugin/issues/538

添加"jsx": "react"到jsconfig。Json不适合我,但“jsx”:“preserve”做到了:

jsconfig。Json / tsconfig.json:

{
  "compilerOptions": {
    "jsx": "preserve"
  }
}