134

I am using typescript to write redux application.

var item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

export default class ItemList extends Component<any, any> {
    render() {
        return (<item />)
    }
}

Then typescript complains this:

Property 'item' does not exist on type 'JSX.IntrinsicElements'.
roger
  • 9,063
  • 20
  • 72
  • 119
  • 19
    try use this with pascal case. Its case sensitive – Rajeesh Madambat May 24 '16 at 12:58
  • @RM-123 No, it can be `item`. `item` with lowercase is a valid HTML element name. Custom Elements can all be lowercase elements just like the intrinsic ones. – trusktr Feb 17 '17 at 02:07
  • 3
    @trusktr No, it _can't_ be `item`. Custom elements _must_ contain `-` a hyphen. [link](https://stackoverflow.com/questions/22545621/do-custom-elements-require-a-dash-in-their-name) – Qwerty Jul 17 '18 at 08:23
  • @Qwerty Doh, I knew that. I think I meant that it is valid to write arbitrary element names in the DOM. You can write `` and it will be there. But there's no way to register a `` element. – trusktr Aug 06 '18 at 04:37
  • 2
    @trusktr No, it is **not valid** to write arbitrary element names in lowercase (without the hyphen) even though it will probably compile and work. – Qwerty Aug 06 '18 at 13:29
  • It'd be nice if it were, because it is valid HTML. – trusktr Aug 14 '18 at 20:23

12 Answers12

290

Your component must start with a capital letter I instead of small letter i otherwise TypeScript would yell. Changing item to Item should fix it:

var Item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

export default class ItemList extends Component<any, any> {
    render() {
        return (<Item />)
    }
}
deadcoder0904
  • 7,232
  • 12
  • 66
  • 163
Rajeesh Madambat
  • 3,231
  • 1
  • 11
  • 18
  • 92
    To be clear to future face-palmers, the trick is the capital letter `I` in `Item`. We're all here having the same lapse in judgement here trying to camelCase a react element. Chances are if you're reading this you're sleep deprived enough to miss that capital letter too. – Kavi Siegel Feb 22 '17 at 19:52
  • Just as reference: this also applies to Functional Components. Function name should be capitalized as well. – Miquel Canal Jan 14 '22 at 18:48
  • here i am again a month later (face palm) – JesseBoyd Mar 08 '23 at 01:05
  • 1
    Can you just turn that nonsense off? – Patrick Jun 09 '23 at 02:26
59

You can declare your custom element type like this:

import * as React from 'react'

declare global {
  namespace JSX {
    interface IntrinsicElements {
      item: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
    }
  }
}
Brian Di Palma
  • 7,043
  • 3
  • 19
  • 15
Sinapcs
  • 2,495
  • 1
  • 20
  • 23
  • 1
    Great answer. Just to add in case it's not obvious, if your custom component is named like "my-item" then the syntax is ['my-item']: React.DetailedHTMLProps... – caprica Nov 13 '22 at 13:52
  • 1
    For Next.js add it to a file filename.d.ts and don't forget to include it in tsconfig.json "include": [... "filename.d.ts"], – Matej Vukosav Nov 15 '22 at 22:54
37

That is because your item component's name does not start with a capital letter, causing Typescript to complain. Replacing item with Item could solve this problem.

James Wilkins
  • 6,836
  • 3
  • 48
  • 73
jsina
  • 4,433
  • 1
  • 30
  • 28
28

For anyone else stuck on this.

The solutions was to

rm -rf node_modules
npm install

Typescript complained

Property 'div' does not exist on type 'StyledInterface'.

and

Property 'div' does not exist on type 'JSX.IntrinsicElements'.

I think the root cause was vscode (me) accidentally editing type definitions in node_modules .

Jkarttunen
  • 6,764
  • 4
  • 27
  • 29
  • 8
    I concur, vscode does rename the type definition when one use the refactoring feature on a component, e.g. ` – Édouard Lopez May 04 '21 at 13:01
  • 3
    Said in another way, you pressed F2 on a tag to change it to something else and you literally renamed in in the type definitions. `npm uninstall @types/react` and `npm install --save-dev @types/react` did it for me. – antirealm Aug 11 '22 at 09:20
  • I guess it can happen that way too, i think i did find/replace – Jkarttunen Aug 21 '22 at 09:26
  • In addition to downloading a fresh copy of node_modules, you may need to restart VS Code for the errors to go away. – esinator May 31 '23 at 19:47
9

It looked like I had the same problem as in this question, but it was typo :/ But I've decided to share that here since error looked very similar and google led me here.

I had typo in declaration:

declare global {
    namespace JSX {
        interface IntrinisicElements {
            'about-me': { me: string }
        }
    }
}

Instead of IntrinsicElements I had IntrinisicElements (extra 'i' letter after 'Intrin'). The compiler didn't complain that because it is definition of interface, so the name can be of our choice.

And the error was like that:

Property 'about-me' does not exist on type 'JSX.IntrinsicElements'.

Bronek
  • 10,722
  • 2
  • 45
  • 46
7

Remember: "item" is not valid in TypeScript, you must declare as "Item", UpperCase the first letter! It is will be like:

var Item = React.createClass({
  render: function() {
    return (<div>hello world</div>)
  }
});

Good day!

Jamviet.com
  • 307
  • 3
  • 11
5

For anyone else who has run into this and the previous answers did not work, this can also happen in a .tsx file due to a misconfiguration of your tsconfig.json file.

I had the error with:

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

And solved it by changing it to just:

...
"compilerOptions": {
   "jsx": "react"
}
...

Again, the above answers are probably the first thing to check, but if you're getting these errors on standard HTML elements like div, span, etc and you're using Typescript. Check your tsconfig.json!

FooBar
  • 419
  • 5
  • 14
4

This could happen if you just renamed a tag by pressing F2 on it in VSCode, which could change the typing of the tag.

When the rename happened, some index.d.ts file should have opened in a new tab. Check if the tab is still open. The path might be something like .../node_modules/@types/react/index.d.ts.

If you find the tab, go into it, then press ctrl-z (cmd-z) to undo your change.

If you don't see the tab, you probably have closed it. Try to use your hotkey to reopen recently closed tabs. If you don't know the hotkey, use command palette View: Reopen Closed Editor. Keep doing it until you find the file, then undo your change. (VSCode remembers the change history for undo, even after you close the tab)

ZYinMD
  • 3,602
  • 1
  • 23
  • 32
2

For all those who faced similar error:

Property 'div' does not exist on type 'JSX.IntrinsicElements'.

Or any other tag instead of div. If other solutions didn't help, try adding to tsconfig.json:

...    
"compilerOptions": {
    "moduleResolution": "node"
    }
...
Sin Ber
  • 21
  • 1
0

I used PascalCase in writing the name of component then the error disappear

-1

Your issue is specifically related to the item's first letter being small.

Problem:

But for those people who are facing this issue due to Creating the React project using Vite instead of CRA, then let me tell to those that Vite has issue with react-ts template and throwing errors in even ts.config.json files

Solution:

So if you want to have react-ts template then please use CRA for that instead of Vite, otherwise for simple react app creation Vite is best.

Thanks

ShoaibShebi
  • 59
  • 1
  • 5
-4

you just edit the name component to capitalize,
for ex:
before: export default function jobAndIncome() {};
after: export default function JobAndIncome() {};

greybeard
  • 2,249
  • 8
  • 30
  • 66