Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Errors while converting a React project

This is specific to a React app I am converting from standard ES2015 to Typescript. In my .tsx files (converted from regular .js files) I am getting a couple of typescript errors I do not know how to solve.

The first is: error TS2339: Property 'propTypes' does not exist on type 'typeof App'.

This applies to:

App.propTypes = {
  actions: PropTypes.object.isRequired,
  fuelSavingsAppState: PropTypes.object.isRequired
};

I tried creating an interface above that code that defined App like:

interface App {
    propTypes?: any;
}

But this had no effect. Clearly I am new to Typescript so what am I missing?

The other error is:

error TS2605: JSX element type 'Element' is not a constructor function for JSX elements.
  Property 'render' is missing in type 'Element'

and this applies to:

{settings.necessaryDataIsProvidedToCalculateSavings ? <FuelSavingsResults savings={settings.savings} /> : null}

The error specifically refers to the bit about <FuelSavingsResults

At a complete loss as to where to even start with that. Any suggestions are most welcome.


in case it helps, the definition for FuelSavingsResults starts like:

let FuelSavingsResults = (props) => {
    let savingsExist = NumberFormatter.scrubFormatting(props.savings.monthly) > 0;
    let savingsClass = savingsExist ? 'savings' : 'loss';
    let resultLabel = savingsExist ? 'Savings' : 'Loss';

and then has a regular React render method that follows, but I don't think that will matter.

like image 427
rg88 Avatar asked Nov 28 '22 20:11

rg88


1 Answers

error TS2339: Property 'propTypes' does not exist on type 'typeof App'.

Instead of

class App extends React.Component{
}
App.propTypes = {
  actions: PropTypes.object.isRequired,
  fuelSavingsAppState: PropTypes.object.isRequired
};

Use a static property.

class App extends React.Component{
    static propTypes = {
      actions: PropTypes.object.isRequired,
      fuelSavingsAppState: PropTypes.object.isRequired
    };
}

error TS2605: JSX element type 'Element' is not a constructor function for JSX elements. Property 'render' is missing in type 'Element'

This was a bug in the compiler that has since been fixed : https://github.com/Microsoft/TypeScript/issues/6349 Alternative provide type annotations for props. 🌹

like image 50
basarat Avatar answered Nov 30 '22 10:11

basarat