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


当前回答

如果你在运行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

其他回答

如果你在运行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

我得到了同样的错误,我只是想办法解决它。问题是有一个jsconfig。tsconfig. json文件导致TypeScript编译器忽略tsconfig. json文件。json文件。

要确定你是否有同样的问题,在你的IDE中(我使用VS Code),在你的编辑器中加载一个有错误的文件,然后打开命令面板,输入“TypeScript: Go To Project Configuration”。如果它打开jsconfig。json,然后删除该文件并重新启动IDE。如果它打开tsconfig。Json文件这次,你是黄金。

添加 { " compilerOptions ": { “jsx”:“反应” } } “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": ["./"]
}

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

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

然后重新启动VS Code。

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

我不需要任何其他步骤。