Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Property 'props' does not exist

I have this .tsx file:

import React, { Component } from 'react';

export class SidebarItem extends Component {
  constructor(props) {
    super(props);
  }
  
  render() {
    return (<li>{this.props.children}</li>);
  }
}

However, TypeScript throws this error:

error TS2339: Property 'props' does not exist on type 'SidebarItem'.
like image 214
Knight Yoshi Avatar asked Jun 25 '17 16:06

Knight Yoshi


3 Answers

The solution is to install the React Types defintions

yarn add -DE @types/react

More details from the typescript docs and from the types repo

On a side note I had to restart vscode for the linting to kick in properly.

like image 181
stilllife Avatar answered Nov 12 '22 20:11

stilllife


TypeScript follows the ES-module specification but React follows CommonJS. This article touches on that among other things.

Importing React like this will fix this problem:

import * as React from 'react';

export class SidebarItem extends React.Component {
    constructor (props) {
        super(props);
    }

    render () {
        return (<li>{this.props.children}</li>);
    }
}
like image 45
Jaap Avatar answered Nov 12 '22 18:11

Jaap


You can try the following way of writing a React Comp.

interface SidebarItemProps
{
    children: any
} 

class SidebarItem extends React.Component<SidebarItemProps, any> { 
    //your class methods 
}

More about using React in TypeScript

like image 6
SteveKitakis Avatar answered Nov 12 '22 18:11

SteveKitakis