The google closure compiler does this. You can optionally annotate your javascript functions with type information (JavaDoc style) and the compiler will enforce it. For example:
/**
* @param {Function} fn
* @param {string|null|undefined} str
*/
foo.bar = function(fn, str) {
// do something
}
It is pretty powerful, you can also do some generics-like stuff like "@param {Array.<string>}" to enforce an array with only strings as elements.I like this idea in theory at least. I think Dylan worked this way too.
It's kind of shame Dylan died out. It seems like it was ahead of its time in many ways.
Dylan does work in a similar way. One issue is that we don't go far enough yet, particularly with functions, so while you can specify a vector of integers, you can't (yet) specify a function that takes an integer parameter, just a function. We'd like to do something about that this year.
Recently, there is some activity to revive Dylan.
I haven't had the chance to try this out yet. I'm sure some people like the fact that their programming language won't put up with fast-and-loose code -- you probably don't want your workout buddy to agree too eagerly to your suggestion that you skip the gym and grab a milkshake instead. But I think it might work for me. I'd be interested to hear about people's experiences, at any rate.