How to solve "JSX element implicitly has type 'any' " error?
Asked Answered
S

23

167

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

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

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

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

Sixpence answered 7/6, 2016 at 8:17 Comment(0)
C
401

I solved this issue by reloading VSCode.

Ctrl+Shift+P -or- +shift+P

Then type: Developer: Reload Window

Cindicindie answered 18/9, 2021 at 0:45 Comment(12)
I had this issue after upgrading from react-dom 16.x to 17. Reloading the window fixed the issue. Thanks!Reine
Restarting TypeScript Language Server would probably be enough: CMD+P > Typescript: Restart TS Server (type in rets)Germinative
it is ctrl+shift+PAksoyn
For me it was cmd + shift + p It looks like cmd + p without shift brings up file search for meLukasz
In VSCode the > character denotes a command. Alternatively, yes, you can use Ctrl + Shift + p and then type in Developer: Reload Window.Cindicindie
Worked like a charm. I should've tried this before Googling.Stricklin
Or just Crtl +R ?Glide
Also closing and reopening fixes this.Electrolyte
I've changed the answer to Ctrl+Shift+P because people were forgetting to use > after doing Ctrl+P.Cindicindie
It would be good to understand what the underlying issue is. Does anyone understand what that was?Kafir
I resolved this issue by deleting the project repository and cloning the repository again and running npm install command then start the project and it started working for me.Histaminase
if you are using typescript, this will not help, you need to install the @types/react to fix thisAtaractic
V
137

I resolved this issue by installing the react type definition.

Try to run: npm install --dev @types/react or yarn add --dev @types/react

Vivianna answered 26/11, 2019 at 8:33 Comment(4)
For npm, npm install --dev @types/react is workedBisexual
If you are using yarn with Typescript and do have node_modules, add Typescript support for your editor using Yarn. Run: yarn dlx @yarnpkg/sdks vscode. VSCode will prompt you to allow it.Manfred
Do not forget to install Node firstBullard
Thank you so much for sharing your solution! Your help is greatly appreciated and has made a significant difference for those who were facing the same issue. Your contribution to the community is valuable and your generosity in sharing your knowledge is commendable. Thanks again for your support! it worked for me.Rote
J
25

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

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

Judaism answered 23/6, 2016 at 19:48 Comment(2)
I really wish they'd consolidate their libraries. Having to deal with global \ ambient \ main conflicts in multiple places is an endless hassle for our team :(Sixpence
I totally agree. I wish more vendors would publish their typings embedded in their libraries (like ImmutableJS)Judaism
T
24

This is happening because your Typescript IntelliSense is not working properly.

1: To fix this just close and reopen your code editor and everything works like a charm!

2: There is another quick turnaround for this issue is just press (Windows: ctrl + shift + p) or (Mac: cmd + shift + p) then search Typescript:Restart TS server hit enter and done.

Tham answered 19/5, 2022 at 5:4 Comment(1)
Tried this on Windows and works like a charm, thanksEpanodos
C
20

Try run this:

1. npm i @types/react --save-dev

2. npm i @types/react-dom --save-dev

Works for me.

Crissycrist answered 11/10, 2021 at 8:54 Comment(1)
Worked for me too! It's also proposed by VS Code when you hover over the marked text.Rundgren
F
6

Run:

npm i -D @types/react @types/react-dom

Then make sure you have the following in your tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react-jsx"
  }
}

This should ideally solve the problem, if not, try reloading the window.

Cheers!

Fougere answered 7/4, 2022 at 13:17 Comment(0)
T
4

You can install @types/react and @types/react-dom and then restart the window.

Step 1. npm i @types/react --save-dev

Step 2. npm i @types/react-dom --save-dev

Step 3: Run Ctrl+Shift+P -or- ⌘+shift+P

Then type: Developer: Reload Window and press enter.

enter image description here

Toolis answered 30/5, 2023 at 10:23 Comment(1)
I was playing with a React (TypeScript + SWC) app generated with vite and it didn't install react-dom. Step #2 helped here.Daffie
G
3

If you are using Vue 2 with "Volar" (Vue 3), disable it for the workspace.

Glide answered 10/5, 2022 at 13:24 Comment(0)
L
3

Need to add vscode support if you are using yarn as your package manager.

Run the code below in your project's terminal:

yarn dlx @yarnpkg/sdks vscode

See https://yarnpkg.com/getting-started/editor-sdks.

Luftwaffe answered 13/5, 2023 at 12:46 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Briolette
thanks! works without the fullstop at the end yarn dlx @yarnpkg/sdks vscodeAuricular
Link to docs: yarnpkg.com/getting-started/editor-sdksContrite
T
2

For people getting this error in Deno or while getting started with Fresh framework a fiw for me was to disable the Deno extension in VS Code

Thomasinethomason answered 19/8, 2022 at 11:7 Comment(0)
K
2

Just Reload the IDE/Code Editor. It will work fine.

Kenwood answered 16/5, 2023 at 12:48 Comment(0)
P
1

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

{
    "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"
    ]
}
Phlebosclerosis answered 4/10, 2017 at 17:42 Comment(2)
must be "jsx": "react-jsx"Mcelroy
React docs state that "jsx": "preserve" should suffice for most applications. Using this setting eliminated a couple other false errors for me.Inflame
A
1

I had a similar issue and after reading the other solutions I looked at my tsconfig.json and apparently it (vs-code) complained about a minor error in my config and when I corrected that, it went away.

So main take away: Check that you have no errors in the tsconfig

Note
I am not sure the error was relevant, but vs-code complained that checkJs was defined (no set to false) when my allowJs was set to false. So basically my solution was to set both allowJs and checkJs to false:

{
  "compilerOptions": {
    ...
    "allowJs": false,
    "checkJs": false,
    ...
  }
}
Airhead answered 12/8, 2021 at 11:27 Comment(1)
Which just disables type checking altogether for .js files.Inflame
C
1

When this happened to me, the cause was running the app inside a docker image, but not having vscode point to that docker image. I ran npm run install to install the node_modules directory where vscode can see it. After that, the error disappeared.

Cran answered 10/10, 2022 at 22:39 Comment(0)
B
0

I faced the same issue, updated the tsconfig.json into the below code and restarted the server worked for me.

{
  "compilerOptions": {
    "target": "ES2016",
    "declaration": false,
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "allowJs": false,
    "preserveConstEnums": true,
    "removeComments": true,
    "sourceMap": true,
    "typeRoots": [
      "./node_modules/@types"
    ],
    "keyofStringsOnly": true,
    "lib": ["es2015", "es2017", "dom"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "jsx": "react"
  }
}
Brittenybrittingham answered 25/4, 2021 at 5:3 Comment(0)
S
0

I also needed to add npm i @types/react-dom --save-dev to fix this.

Stepchild answered 22/9, 2021 at 12:51 Comment(0)
N
0

ctrl + shift + p will Reload the window.

If using TypeScript using ctrl + shift + p will Reload TS Server

Namedropper answered 31/10, 2022 at 6:25 Comment(0)
C
0

If you came here after updating vs code. Then you need to change the version of the typescript used in the settings. example

Couturier answered 4/4, 2023 at 16:36 Comment(1)
I think that this is more a lack of the react library package. the typescript error would be correct, but it's missing information. @types/react covers this and adds the types typescript expects.Edie
F
0

I got the problem in a Next.js project (v13.4.12).

After creating the project, this is what's inside package.json

package.json:

{
  "name": "01-firsts-steps",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@types/node": "20.4.5",
    "@types/react": "^18.2.18",
    "@types/react-dom": "^18.2.7",
    "autoprefixer": "10.4.14",
    "eslint": "8.46.0",
    "eslint-config-next": "13.4.12",
    "next": "13.4.12",
    "postcss": "8.4.27",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "tailwindcss": "3.3.3",
    "typescript": "5.1.6",
    "valibot": "^0.8.0"
  }
}

In page.tsx i got the warning:

JSX Element implicitly has any type 'any' because no interface 'JSX.IntrinsicElements' exists

To solve it, what I did was uninstall the packages: @types/react and @types/react-dom, using:

npm uninstall @types/react @types/react-dom

Finally, reinstall the packages:

npm install @types/react @types/react-dom

And that's it !

Forrestforrester answered 1/8, 2023 at 19:43 Comment(0)
C
0

Run npm install this fixed issue for me

Carlin answered 26/11, 2023 at 15:19 Comment(0)
S
0

for me, the reason was I forgot to run npm install after I cloned a NextJS project

Sori answered 17/4, 2024 at 10:58 Comment(0)
H
-1

Delete node_modules and run npm install

I could not pinpoint a specific issue, and doing this fixed the issue for me.

Hestia answered 17/1, 2024 at 17:10 Comment(0)
S
-9

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

Sixpence answered 7/6, 2016 at 11:53 Comment(1)
Thought it was worth letting you know why you (or, future readers) got downvoted: only React components must use a capital letter, not html elementsAerograph

© 2022 - 2025 — McMap. All rights reserved.