codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Class Check #154

Closed jaulz closed 8 years ago

jaulz commented 8 years ago

I would like to check the class of a parameter in a function i.e. check that it is of a specific class.

import App from './App'

export default decorator(componentToDecorate:App):Function => DecoratedComponent

However if I try to run decorator(App) in my tests it fails:

TypeError: Value of argument "componentToDecorate" violates contract.

Expected:
App

Got:
Function {
  propTypes: {
    actions: Function;
    configuration: Function;
  };
  defaultProps: {
    actions: Object;
    configuration: Object;
  };
}

App is in this case an ES6 class that extends the React Component class. I ran through all the flow documentation but could not find what's wrong here. Is there a simple solution for this?

Many thanks!

phpnode commented 8 years ago

This is happening because your type annotation is specifying an instance of App, not the App class itself. One of these should work:

export default decorator(componentToDecorate:Class<App>):Function => DecoratedComponent
// or...
export default decorator(componentToDecorate:typeof App):Function => DecoratedComponent