Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created July 12, 2014 20:57
Embed
What would you like to do?
// from https://news.ycombinator.com/item?id=8024116
function checkType(f) {
return function(a) {
var type = f.toString().match(/\/\/(.*)\n/)[1].trim();
if(type !== typeof(a)) throw new Error('Invalid type');
return f(a);
}
}
var halve = checkType(function(n) {
// number
return n / 2;
});
halve(4); // returns 2
halve('string'); // emits error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment