Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript complains Property does not exist on type 'JSX.IntrinsicElements' when using React.createClass?

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'. 
like image 213
roger Avatar asked May 24 '16 12:05

roger


People also ask

Does not exist on type JSX IntrinsicElements typescript?

The error "Property does not exist on type 'JSX. IntrinsicElements'" occurs when a component's name starts with a lowercase letter. To solve the error, make sure to always start component names with a capital letter, install the types for React and restart your dev server.

What is JSX intrinsic elements?

Type Checking For React, intrinsic elements are emitted as strings ( React. createElement("div") ), whereas a component you've created is not ( React. createElement(MyComponent) ). The types of the attributes being passed in the JSX element should be looked up differently.


2 Answers

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 />)     } } 
like image 179
Rajeesh Madambat Avatar answered Sep 21 '22 17:09

Rajeesh Madambat


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>;     }   } } 
like image 43
Sinapcs Avatar answered Sep 20 '22 17:09

Sinapcs