0

In React using Typescript we can do this:

  constructor(props: Props) {
    super(props);
    this.state = {
      teams: []
    };
  }

To explicitly let the typescript compiler know which props will be passed to the constructor lifecycle method. But we have already let the component know which props are available in the component in the class declaration

extends Component<Props, State> {

So it seems redundant to specify this again in all lifecycle methods. It seems that the Component types file support this(@types/react v. 16.4.16):

    class Component<P, S> {
    constructor(props: Readonly<P>);

So the question is why does this not work? (Typescript v. 2.9.2)

enter image description here

TamRock
  • 1,490
  • 1
  • 11
  • 27
  • Yeah, this is just not supported. Methods can define whatever parameter types they want, they will be checked for compatibility against the base class but no inference for the parameters will occur. – Titian Cernicova-Dragomir Oct 15 '18 at 09:07
  • This seems to be the same question, doesn't it? https://stackoverflow.com/questions/52802807/how-to-avoid-redeclaring-props-type-in-react-constructor-using-typescript/52803585#52803585 – Estus Flask Oct 15 '18 at 09:09

0 Answers0