我已经四处寻找解决这个问题的办法了。他们都建议在你的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://staxmanade.com/2015/08/playing-with-typescript-and-jsx/

参考章节: 修复错误TS17004:除非提供了'——JSX '标志,否则不能使用JSX。

下一个错误对我来说是新的,但它有一定的意义,所以我将——jsx标志添加到tsc,并尝试tsc——jsx helloWorld。tsx,但看起来我错过了一个参数——jsx。

tsc—jsx helloWorld.tsx '——jsx'的参数必须是'preserve'或'react'。 在当前的TypeScript 1.6迭代中,——jsx似乎有两个选项,分别是preserve或react。

Preserve将在输出中保留JSX。我认为这是为了让您可以使用JSX之类的工具来实际提供翻译。 react将删除jsx语法,并将其转换为纯javascript,因此在TSX文件中将成为react。createElement (" div ", null)。 通过传递react选项,这里是我们结束的地方:

tsc—jsx react helloWorld.tsx tsx(11,14):错误TS2607: JSX元素类不支持属性,因为它没有“props”属性 helloWorld.tsx(11,44):错误TS2304:无法找到名称“mountNode”。 接下来我将处理最后一个错误,因为最初我不理解上面的JSX错误。

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

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

2021年4月,这个问题仍然存在。我使用react-boilerplate-cra-template与react 17.02。

这是VSCode的一个问题,它默认使用内置的typescript。你只需要打开一个。tsx文件

按:Ctrl + Shift + P 输入:Typescript:选择Typescript版本… 选择:使用工作区版本。

我必须把我的整个应用程序添加到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服务器并不能解决我的问题。

我不需要任何其他步骤。