typescript 错误 TS2602:JSX 元素隐式具有类型“any”,因为全局类型“JSX.Element”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37674017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:33:32  来源:igfitidea点击:

error TS2602: JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist

reactjstypescripttypescript-typings

提问by Richard

Even though I've installed & referenced the Typings library 'react' like this

即使我已经像这样安装并引用了 Typings 库“反应”

/// <reference path="../../../typings/browser.d.ts" />

/// <reference path="../../../typings/browser.d.ts" />

I'm still getting the error below: enter image description here

我仍然收到以下错误: enter image description here

Is there another Typings library I should be installing as well?

我还应该安装另一个 Typings 库吗?

回答by darthtrevino

There are two versions of React available in Typings. I've seen this issue with typings install reactand using noImplicitAny.

Typings 中有两个版本的 React 可用。我已经看到这个问题typings install react并使用noImplicitAny.

I resolved this by installing the global version: typings install dt~react --globaltypings search react results

我通过安装全局版本解决了这个问题: typings install dt~react --globaltypings search react results

回答by Daniel Dyrnes

I resolved this issue by installing the react type definition.

我通过安装 react 类型定义解决了这个问题。

Try to run yarn add --dev @types/react.

尝试运行yarn add --dev @types/react

回答by Daniel Dyrnes

Put this configuration in the tsconfig.json file so that the ts server does not recognize that type of errors

将此配置放在 tsconfig.json 文件中,以便 ts 服务器无法识别该类型的错误

{
    "compilerOptions": {
        "outDir": "build/dist",
        "module": "commonjs",
        "target": "es5",
        "lib": ["es6", "dom"],
        "sourceMap": true,
        "allowJs": true,
        "jsx": "react",
        "moduleResolution": "node",
        "rootDir": "src",
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitAny": true,
        "strictNullChecks": true
    },
    "exclude": [
        "node_modules",
        "build",
        "scripts",
        "acceptance-tests",
        "webpack",
        "jest",
        "src/setupTests.ts"
    ],
    "types": [
        "typePatches"
    ]
}

回答by Richard

From the handbook, React must have a capital letter. The code in the question clearly does not.

手册中,React 必须有一个大写字母。问题中的代码显然没有。