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

Exported Types #35

Closed qpre closed 8 years ago

qpre commented 8 years ago

Having my type declared as:

// myType.es6
export type Type = {
  id:  number,
};

I'd want to use it this way:

// myImplem.es6
import type {Type} from 'myTypeFile';

export function get(id) : Type {
  let j: Type = { id };
  // do stuff
  return j;
}

but these respectively transform into:

// myType.js
"use strict";

exports.__esModule = true;
// myImplem.js
'use strict';

exports.__esModule = true;
exports.get = get;

function get(id) {
  var j = {
   id: id.
  };

  if (!(j instanceof Type)) throw new TypeError('Function \'get\' return value violates contract, expected Type got ' + (j === null ? 'null' : j instanceof Object && j.constructor ? j.constructor.name : typeof j));
  return j;
}

Which quite logically produces this error at runtime.

ReferenceError: Type is not defined

Any idea of whether it is more likely to be an error in my configuration or a bug in the tool ?

phpnode commented 8 years ago

It's a shortcoming of babel which will be addressed in v6 - babel doesn't let us cross module boundaries yet. When it does we'll start supporting typedefs

phpnode commented 8 years ago

closing because this is a dupe of #2

qpre commented 8 years ago

Thanks.