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

Support for Disjoint Unions? #156

Closed ajihyf closed 7 years ago

ajihyf commented 8 years ago

Hi, I'm using a disjoint union type like this

type ASTNode = ASTProgramNode | ASTLiteralNode

type ASTProgramNode = {
  type: AST.Program,
  body: ASTNode
}

type ASTLiteralNode = {
  type: AST.Literal,
  value: any
}

class AST {
  static Program: string = 'Program'
  static Literal: string = 'Literal'

  constant (): ASTLiteralNode {
    return { type: AST.Literal, value: this.tokens[0].value }
  }
}

However the plugin throws an error Literal is not a valid operator for instanceOf. It seems that the plugin simply transform type annotations to instanceOf for runtime type checking.

phpnode commented 7 years ago

Hi, sorry for taking so long to respond to this, this project is now deprecated in favour of https://codemix.github.io/flow-runtime which aims for full compatibility with Flow.

I checked and babel-plugin-flow-runtime does not seem to have this particular bug, but it doesn't yet handle the hoisting of the type declarations correctly, I've opened an issue here: https://github.com/codemix/flow-runtime/issues/1