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


当前回答

对于使用Visual Studio 2019的人,您可以通过执行以下所有操作来修复它:

在tsconfig。Json,有这部分

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

更新C:\Program Files (x86)\Microsoft SDKs\ Typescript中的Typescript sdk 我有4.0版本。我需要将它更新到4.1(更清楚地说,是4.1.2) 删除Typescript文件夹 在Visual Studio 2019 Extensions > Manage Extensions中,安装Typescript 4.1 for Visual Studio

其他回答

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

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

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

每次我运行npm start时,它都会覆盖我在{jsx:…}使用React - JSX,以便与React 17中的JSX转换兼容。

The following changes are being made to your tsconfig.json file:
  - compilerOptions.jsx must be react-jsx (to support the new JSX transform in React 17)

问题是VSCode使用了旧版本的typescript(4.0.3),而项目附带的typescript版本是(4.1.2)。

下面的方法对我很有效:

转到命令面板CTRL+Shift+P(或Mac上⌘+Shift+P)。 选择“TypeScript: Select a TypeScript Version…” 选择“使用工作区版本”。

PS:这个选项不会显示,除非你在任何。tsx文件(谢谢@awran5的评论和很好的捕捉)

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

> 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运行您的项目

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

崇高文本的解决方案:

Make sure your package.json uses Typescript >= v4.1.2 Uninstall the Sublime Text "Typescript" package through Package Control At time of writing, it bundles Typescript v3.x Open a terminal in the Sublime Text "Packages" directory. cd "~/Library/Application Support/Sublime Text 3/Packages" Clone the Typescript-Sublime-Plugin repo into your Packages directory: git clone https://github.com/microsoft/TypeScript-Sublime-Plugin TypeScript Open User Settings: Sublime Text > Preferences > Settings Add "typescript_tsdk": "node_modules/typescript/lib" This tells the Sublime Typescript plugin to use the project version of Typescript rather than its bundled version. Restart Sublime Text Unlike most Sublime Text settings, this one requires a restart in order to restart the Typescript server.

参考: https://github.com/microsoft/TypeScript-Sublime-Plugin/issues/538