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

Babel 6 Support #42

Closed Rleahy22 closed 9 years ago

Rleahy22 commented 9 years ago

Love this plugin and I want to use it with Babel 6, but it does not appear to work. Any plans to support it?

mindjuice commented 9 years ago

It's working for me in Babel 6:

kenc$ babel --version
6.0.12 (babel-core 6.0.12)

I thought it wasn't working at first too, because it didn't give any compile-time warnings.

It seems to be a runtime-only typechecker though. When I checked the compiled code, it clearly has the typecheck transformations in it. For example:

function bar(aNum, anOptionalString // will allow null/undefined
) {
  if (typeof aNum !== 'number') throw new TypeError('Value of argument \'aNum\' violates contract, expected number got ' + (aNum === null ? 'null' : aNum instanceof Object && aNum.constructor ? aNum.constructor.name : typeof aNum));
  if (anOptionalString != null && typeof anOptionalString !== 'string') throw new TypeError('Value of argument \'anOptionalString\' violates contract, expected null or string got ' + (anOptionalString === null ? 'null' : anOptionalString instanceof Object && anOptionalString.constructor ? anOptionalString.constructor.name : typeof anOptionalString));
  if (typeof anOptionalString !== 'number') throw new TypeError('Function \'bar\' return value violates contract, expected number got ' + (anOptionalString === null ? 'null' : anOptionalString instanceof Object && anOptionalString.constructor ? anOptionalString.constructor.name : typeof anOptionalString));
  return anOptionalString;
}

Calling bar('some string'); gave a runtime error.

mindjuice commented 9 years ago

OK, it does do some compile-time checks, like this example from the front page:

function foo2 (): boolean {
  if (Math.random() > 0.5) {
    return "yes"; // <-- SyntaxError - string is not boolean
  }
  else {
    return false;
  }
}

function bar2 (input: string = 123): string { // <-- SyntaxError: default value is not string
  return input + "456";
}

But something like this is not flagged:

function foo(x : string) {
  return x;
}

foo(10);  // Passing number to function that takes a string
phpnode commented 9 years ago

Love this plugin and I want to use it with Babel 6, but it does not appear to work. Any plans to support it?

Absolutely, in fact babel 6 brings with it a lot of improvements which will help us make this plugin a lot better, but it only came out yesterday and came with a lot of breaking changes for plugin authors, so yes, but give us chance!

phpnode commented 9 years ago

@mindjuice it definitely doesn't work with 6 yet, so you probably have something wrong with your config

Rleahy22 commented 9 years ago

Haha, sorry didn't mean to rush you.

mindjuice commented 9 years ago

Hmmm...weird. I'm guessing that the webpack babel loader must be running a different version of babel than what I have on the CLI path.