3 ms·
Agree with you 100%. One could use this system for asserts though: assert(type.isObject({}))); assert(type.isNumber(NaN)); // fails assert(type.is
by annnnd 13y ago
Agree with you 100%.
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.
- ghiculescu 13y agoIt's still a lot of code being run without real benefit. Use Typescript if you really want static types, write sufficient unit tests either way, and you don't need this.
- rimo 13y agoStatic types and unit tests don't solve or catch all problems. I'm thinking about event-driven messaging systems, which is pretty common in js; Static typing will not be of any help in that situation.
- embwbam 13y agoYou can use a different observer pattern sometimes that can be caught by static typing, like Signals
- danbruc 13y agoSufficiently advanced type systems can enforce an enormous range of constraints. You can for example create a type for trees enforcing that the tree must be balanced; code that would unbalance a tree will just not compile.
- annnnd 13y agoSure - and you can test types in your unit tests using the OP's technique. :)