我已经四处寻找解决这个问题的办法了。他们都建议在你的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预设。如果你们需要更多的文件来帮助调试,请告诉我。


当前回答

重新启动我的IDE没有帮助。重启TypeScript服务器确实解决了这个问题。

其他回答

我遵循了这里提到的“更简单的解决方案”https://github.com/facebook/create-react-app/issues/10144#issuecomment-733278351

"...更新包中的TS版本。Json到4.1.2"

然后重新启动VS Code。

简单地用命令面板重新启动TS服务器并不能解决我的问题。

我不需要任何其他步骤。

即使在运行npx tsc——jsx preserve时,我也得到了这个错误,所以——jsx是明确指定的。

在我的例子中,这是因为在tsconfig中有incremental: true。显然,在增量模式下,tsc可能会忽略——jsx设置,而使用以前构建的信息(其中——jsx仍然被禁用)。作为一种解决方案,我暂时转向增量编译,重新编译,然后重新启用它。也许删除构建构件也可以工作。

对于使用Visual Studio 2019的人,您可以通过执行以下所有操作来修复它:

在tsconfig。Json,有这部分

"include": [
    "./src/**/*.ts"
]

更新C:\Program Files (x86)\Microsoft SDKs\ Typescript中的Typescript sdk 我有4.0版本。我需要将它更新到4.1(更清楚地说,是4.1.2) 删除Typescript文件夹 在Visual Studio 2019 Extensions > Manage Extensions中,安装Typescript 4.1 for Visual Studio

我必须把我的整个应用程序添加到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": ["./"]
}

对于正在阅读的读者,请转到tsconfig。将这一行从react-jsx改为react:

{
  "compilerOptions": {
    "jsx": "react"
  }
}

额外的好处:尝试将IDE TS版本设置为最新的(atm 4.2),在vscode CMD + SHIFT + P中更改它。