Skip to content

typeof React.Component stopped working #18946

Description

@mikew

TypeScript Version: 2.5.3

Code

class MyComponent extends React.Component<{}, {}> {
  render () {
    return null
  }
}

function thisTakesAComponent (component: typeof React.Component) {

}

thisTakesAComponent(MyComponent)

Expected behavior:
Should compile fine.

Actual behavior:

Argument of type 'typeof MyComponent' is not assignable to parameter of type 'typeof Component'.
  Type 'MyComponent' is not assignable to type 'Component<P, S>'.
    Types of property 'setState' are incompatible.
      Type '{ <K extends never>(f: (prevState: {}, props: {}) => Pick<{}, K>, callback?: (() => any) | undefi...' is not assignable to type '{ <K extends keyof S>(f: (prevState: S, props: P) => Pick<S, K>, callback?: (() => any) | undefin...'.
        Types of parameters 'f' and 'f' are incompatible.
          Types of parameters 'prevState' and 'prevState' are incompatible.
            Type '{}' is not assignable to type 'S'.

Activity

  1. mikew commented on Oct 4, 2017

    @mikew
    Author

    And what about for stateless functions? IE I don't want "a type of subclasses of React.Component", I want "something that will be rendered correctly with React.createElement".

  2. mhegazy commented on Oct 4, 2017

    @mhegazy
    Contributor

    why not:

     function thisTakesAComponent <P>(component: React.SFC<P> | React.ComponentClass<P>) {
     }
  3. mhegazy commented on Oct 19, 2017

    @mhegazy
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  4. samuelneff commented on Mar 11, 2018

    @samuelneff

    Not sure if this was applicable when originally asked, but now you can do this:

        function thisTakesAComponent(component: ReactType) {
        }
  5. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions