Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typeof React.Component stopped working #18946

Closed
mikew opened this issue Oct 4, 2017 · 6 comments
Closed

typeof React.Component stopped working #18946

mikew opened this issue Oct 4, 2017 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mikew
Copy link

mikew commented Oct 4, 2017

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'.
@ghost
Copy link

ghost commented Oct 4, 2017

MyComponent is not of type typeof React.Component, it's of type typeof MyComponent. This probably used to work before #16368, since we ignored the generic type arguments before, which made them indistinguishable.
If you want a type of subclasses of React.Component, you could use something like { new(): React.Component<any, any> } -- of course this depends on how you plan to use the variable.

@mikew
Copy link
Author

mikew commented Oct 4, 2017

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".

@ghost
Copy link

ghost commented Oct 4, 2017

I guess that depends on what react needs available on such a type -- I'm not familiar with the library so I couldn't tell you.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2017

why not:

 function thisTakesAComponent <P>(component: React.SFC<P> | React.ComponentClass<P>) {
 }

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 4, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 19, 2017

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

@mhegazy mhegazy closed this as completed Oct 19, 2017
@samuelneff
Copy link

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

    function thisTakesAComponent(component: ReactType) {
    }

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants