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


当前回答

我的情况下,没有一个解决方案是有效的,所以我看了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文件已经变得更广泛地可用。如果你想了解这些选项,我已经发布了一个更详细的版本作为主旨。

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

jsconfig。Json / tsconfig.json:

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

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

我只是和我的一个朋友一起解决了这个问题,他重新安装和配置了所有该死的东西来解决它。我们尝试了互联网上的所有修复(我甚至不得不自己解决这个问题,所以我真的很困惑为什么我们可以为他修复它)。

问题出在TypeScript的God扩展上。一旦它被禁用,随后被删除,问题就解决了。

当心假神!

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

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

然后重新启动VS Code。

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

我不需要任何其他步骤。