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


当前回答

下面是我在运行纱线启动后得到的错误:

> multi-step-form-typescript@0.1.0 start /home/ehsan/Documents/GitHub/multi-step-form-typescript
> react-scripts start

/home/ehsan/Documents/GitHub/multi-step-form-typescript/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239
      appTsConfig.compilerOptions[option] = value;
                                          ^

TypeError: Cannot assign to read only property 'jsx' of object '#<Object>'
    at verifyTypeScriptSetup (/home/ehsan/Documents/GitHub/multi-step-form-typescript/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)
    at Object.<anonymous> (/home/ehsan/Documents/GitHub/multi-step-form-typescript/node_modules/react-scripts/scripts/start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! multi-step-form-typescript@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the multi-step-form-typescript@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ehsan/.npm/_logs/2020-11-22T18_20_20_546Z-debug.log

你需要做什么来摆脱这个

在终端上单击错误链接

/home/ehsan/Documents/GitHub/multi-step-form-typescript / node_modules / react-scripts /脚本/跑龙套/ verifyTypeScriptSetup.js: 239 appTsConfig.compilerOptions[option] = value;

然后把239行改成

否则(parsedcoleroptions [option] !== valuetoche&& option !== jsx)

现在,在将goto更改为tsconfig.json之后

然后将“jsx”:“react-jsx”替换为“jsx”:“react”

现在用sudo yarn start运行您的项目

这是我的工作,希望这对你也有用:)

其他回答

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

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

然后重新启动VS Code。

简单地用命令面板重新启动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

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

jsconfig。Json / tsconfig.json:

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

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

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

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

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

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

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

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

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