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


当前回答

你可以创建一个。vscode/settings。Json文件在你的项目的根。在里面输入{"typescript。Tsdk ": "node_modules\\typescript\\lib"}你就可以开始了。

问题是vscode使用的是旧的Typescript版本。

在vscode上显示代码的图像

其他回答

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

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

然后重新启动VS Code。

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

我不需要任何其他步骤。

这个链接有助于解决这个问题: 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错误。

在我的情况下,我尝试了所有的tsconfig。json,项目属性对话框,重新启动IDE,检查安装的TypeScript版本等,它仍然有这个错误。找到开发人员,使项目添加条件属性的项目文件,这样TypeScriptJSXEmit没有定义在所有的配置(这混淆了项目属性对话框)。

这是从我的项目文件显示的问题摘录:

...
  <PropertyGroup Condition="'$(Configuration)' == 'QA'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptJSXEmit>React</TypeScriptJSXEmit>
...

崇高文本的解决方案:

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

我的经验

我正在转换现有的新创建的NextJS应用程序使用TS遵循官方的下一步指南,当我到达页面/_app。需要修改TSX。

我按照VSCode提示添加——jsx标志,这将添加以下行到next.config.js文件:

module.exports = {
  reactStrictMode: all,
};

这导致了另一个错误:

Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error
ReferenceError: all is not defined

在删除next.config.js文件中的行并重新启动VSCode并重新运行yarn dev或npm运行dev,瞧!它开始工作了!