cssinjs/jss

React-jss doesn't ignores defaultProps in Typescript

Open

#1106 opened on May 15, 2019

View on GitHub
 (3 comments) (4 reactions) (0 assignees)JavaScript (7,000 stars) (434 forks)batch import
help wantedtypescript

Description

Expected behavior: Typescript 3.0 added support for defaultProps in React.
Using WithStyles seems to break this behavior.

Describe the bug: Props are reported as required even though they are defaulted using a public static defaultProps field.

Codesandbox link: The following code should do it but I can't make it work in CodeSandbox. For some reason, it keeps marking React as any which can't trigger the error.
Here is the url anyway: https://codesandbox.io/s/fk93m

import * as React from "react";
import { render } from "react-dom";
import injectSheet, { WithStyles } from "react-jss";

const styles = {
  blue: {
    color: "blue"
  }
};

interface IProps extends WithStyles<typeof styles> {
  count: number;
}

class App extends React.Component<IProps> {
  public static defaultProps = {
    //count: 18 // Enabling this line triggers a type error when Using RealApp below
  };

  public render() {
    const { classes, count } = this.props;

    return <div className={classes.blue}>The count is {count}.</div>;
  }
}

const RealApp = injectSheet(styles)(App);

render(<RealApp />, document.getElementById("root"));

Versions (please complete the following information):

  • react-jss: ^10.0.0-alpha.16.
  • Browser: Chrome but error is due to typings.
  • OS: Windows
  • Typescript: 3.4.3

Contributor guide