5 ms·
FINALLY, both Ruby and Python have had optional types for a while; glad JS is catching up.
by brianleroux 5y ago
FINALLY, both Ruby and Python have had optional types for a while; glad JS is catching up.
- fooyc 5y agoPHP has optional types as well, although types are enforced when present
- colejohnson66 5y agoThat's a major limitation of JavaScript. For example: // TypeScript function add(a: number, b: number): number { return a + b; } // tsc output (JavaScript) function add(a, b) { return a + b; } // (ab)use by someone using my lib without TypeScript add("abc", "def"); Now, addition is a contrived example because it'll still work with strings, but the point is that such usage violates my code contract because the contract was deleted during compilation. It's possible to do this: function add(a: number, b: number): number { if (typeof a !== 'number' || typeof b !== 'number') throw 'no'; return a + b; } But that's needlessly verbose, and eslint will complain about useless code. If enforceable type annotations could be added to JavaScript, that'd be a huge plus.
- ketzo 5y agoAs I understand this proposal, it would not add what you're talking about. Just for clarity.
- deleted 5y ago[deleted]
- grandpoobah 5y agoExcept that's not what this is. These are just "type comments" that are not enforced at runtime.
- globuous 5y agoAre python type annotations enforced at runtime ? I'm pretty sure they're not
- pcthrowaway 5y agoI thought Python type comments weren't enforced at runtime either.. are they?
- DangitBobby 5y agoNo, they are not.