One could use this system for asserts though:
assert(type.isObject({})));
assert(type.isNumber(NaN)); // fails
assert(type.isElement(document.createElement('div')));
assert(type.isRegExp(/abc/);
This could increase readability (because it documents the data type expectations) and would also break on some errors as soon as possible.Secondly I completely agree about wanting a program to crash noisily and detectably in development, but in production I would prefer that the error is handled and the user can continue to use the app.
Finally I also agree that if type checking is really important to you, a compile to JS language like TypeScript or Dart are the best options. The techniques / approach I am discussing in this article are really for people who already have a large vanilla JS code base or want to learn a bit more about types in JavaScript.
Maybe that's ok if it's a game or something, but this is a dangerous strategy. How do you know it's ok to use the app if there's an error? You have to very carefully isolate and control where your errors can be, and I'm not sure this approach allows you to do that.
interface IA { }
interface IB { foo(): void; }
function fn(passedFn: (a: IA) => void) {
passedFn({});
}
// compiles but throws TypeError at runtime!
fn((b: IB) => b.foo());
I much prefer my systems to crash when a programmer made an error, and do so noisily and detectably, so that the problem can be fixed. I also prefer the actual logic of my code to make up more than 20% of the lines. If you're going to add boilerplate type checks all over, the signal-to-noise ratio gets really bad.
So don't do this. Use Typescript if you want to check types.