我已经四处寻找解决这个问题的办法了。他们都建议在你的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”,它似乎可以处理这个问题,如果再次出现任何问题,我会编辑这条评论

其他回答

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

添加 { " compilerOptions ": { “jsx”:“反应” } } “tsconfig。重新启动编辑器就解决了这个问题。

Create-react-app打破了常规。截至2022年5月

在tsconfig中,include只被设置为/src,更改为:

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

如果你在运行create-react-app with Typescript后看到这个问题,你可以通过添加“Typescript . bat”来解决这个问题。Tsdk ": "node_modules/typescript/lib"到.vscode/settings.json。

对于智能感知,如果你使用"jsx": "react-jsx",你需要切换你的工作空间来使用TS 4.1+

或者从视觉上看,只需转到蓝色任务栏并选择Typescript版本(可能是4.x。x something),然后选择“select TypeScript Version”。

然后选择“Use Workspace Version Version”,引用node_modules/typescript/lib

为了解决未来所有项目的这个问题,你可以为VSCode安装JavaScript和TypeScript Nightly扩展。