1

I have a TypeScript TSX file with a React component like this:

interface IHiProps {
  a: string;
  b: string;
}

interface IHiState {

}

class Hi extends __React.Component<IHiProps, IHiState> {
  // render…
}

How do I correctly render this component in TypeScript?

var hiComponent: __React.Component<IHiProps, IHiState>;
hiComponent = __React.render(__React.createElement(Hi), { a: "A", b: "B" }, document.body);

This fails me with Argument of type 'typeof Hi' is not assignable to parameter of type 'ComponentClass'. I am using definitely typed files from the DefinitelyTyped repository of Boris Yankov.

1 Answer 1

2

Does it work when you change your class like this?

class Hi extends __React.Component<IHiProps, IHiState> {
  static propTypes: __React.ValidationMap<any> = {
    a: __React.PropTypes.string,
    b: __React.PropTypes.string
  }

  // render…
}

Also using __React is suspicious as it is internal variable of React.d.ts. In runtime there would not be such variable - if you would not create it. Consider rather using modules:

/// <reference path="/path/to/react.d.ts" />
import * as React from React;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.