Closed zerkalica closed 8 years ago
I don't like the idea of depending on another module for this, people forget to install it, it's hard to keep up to date with the babel plugin version etc. I want to keep things simple and fast.
However, whether we should support some kind of reflection is a different question, we could do that without requiring an external lib. E.g.
function foo (a: string, b: string): Array<string> {
return [a, b];
}
could compile to:
function foo (a, b) {
return [a, b];
}
foo[Symbol.for('types')] = [["string", "string"], "Array<string>"];
Obviously it'd have to be optional.
We can reduce their size by generating specialized helper functions for each type we need to verify in a particular file. Most of the rest of the size is in the error message which is very tailored to the specific function and not reusable. The other advantage of this approach is that we get accurate stack traces which makes debugging very easy. This is not possible with a typecheck library, and I don't think that using one really buys us anything in practise.
What about reflection types in runtime, for example:
transpiled to
Now we can easy parse types in runtime and extract some info, this bridge for React.proptypes. We can easily build plugins for working with types in runtime.
transpile to
What about using https://github.com/gcanti/tcomb for type checking? Why tcomb? Because it's great runtime type specification. It's powerfull type assertion library with many types, which you reimplement internally in you plugin.