Re:A Proposal for Type Syntax in JavaScript
by brianleroux on Wednesday March 09, 2022 @02:55PM
(#30619212)
FINALLY, both Ruby and Python have had optional types for a while; glad JS is catching up.
[ Parent
| on HN ]
Re:A Proposal for Type Syntax in JavaScript
by grandpoobah on Wednesday March 09, 2022 @06:16PM
(#30621259)
Except that's not what this is. These are just "type comments" that are not enforced at runtime.
Re:A Proposal for Type Syntax in JavaScript
by globuous on Wednesday March 09, 2022 @06:25PM
(#30621332)
Are python type annotations enforced at runtime ? I'm pretty sure they're not
Re:A Proposal for Type Syntax in JavaScript
by pcthrowaway on Wednesday March 09, 2022 @06:23PM
(#30621311)
I thought Python type comments weren't enforced at runtime either.. are they?
Re:A Proposal for Type Syntax in JavaScript
by DangitBobby on Wednesday March 09, 2022 @11:13PM
(#30623141)
No, they are not.
Re:A Proposal for Type Syntax in JavaScript
by fooyc on Wednesday March 09, 2022 @03:56PM
(#30619831)
PHP has optional types as well, although types are enforced when present
Re:A Proposal for Type Syntax in JavaScript
by colejohnson66 on Wednesday March 09, 2022 @04:04PM
(#30619930)
That'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.