'Typing a class component with no props

I am trying to type a class component with no props

import React, { Component } from "react";

type Props = {};

class MyComponent extends Component<Props> {
  constructor(props: Props) {
    super(props);
    this.state = { hasError: false };
  }

  render() {
    return <h1>hello</h1>;
  }
}

export default MyComponent;

It seems like I just cannot ommit them

import React, { Component } from 'react';

class ErrorBoundary extends Component {
  constructor() {
    super();
    this.state = { hasError: false };
  }

What is the correct way to type a class component with no props?



Solution 1:[1]

You can initialize the state like this, avoiding to (manually) overwrite the constructor.

class ErrorBoundary extends Component {
  state = {
    hasError: false
  }

  render() {
    return <h1>hello</h1>;
  }
}

I think that's what you're talking about, not the typings.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas