3 ms·
I came from C#/Java to JavaScript and it was extremely painful. I didn't care that much about typing until I experienced a dozen types of error that should have
by andai 3y ago
I came from C#/Java to JavaScript and it was extremely painful. I didn't care that much about typing until I experienced a dozen types of error that should have been impossible. (Shouldn't even compile, let alone silently poison your data at runtime...)
For years I thought it was a dynamic/static issue. Then I used Python (without type hints), and almost all the JS errors were impossible in Python too! Turns out it's about weak/strong typing instead.
Put simply: when given something that is an obvious error, throw an error. This sounds obvious, but given an obvious error, in many cases JavaScript remains silent and passes garbage data to the rest of the program.
I think if JS added a "use strong", it would remove 80-90% of the need for TypeScript.
- yett 3y agoJavascript does have 'use strict' https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- andai 3y agoYes! Exactly. However: "use strict"; let undef; let str1 = "hello"; let str2 = str1 + undef; console.log(str2); // -> "helloundefined" Whereas I propose: "use sane"; let undef; let str1 = "hello"; let str2 = str1 + undef; // -> Uncaught TypeError: Cannot append string and undefined console.log(str2); // (code never reaches this point) (I would also settle for "Uncaught ReferenceError: Attempted use of `undef` before initialization.")
- cmrdporcupine 3y agoYeah, JavaScript has a pile of terrible type coercions, implicit conversions and misuse/reuse of operators. It was a bad 90s/00s trend to do this kind of thing; re-use + for string append, or coerce things into strings for convenience. Python and Ruby have their own gotchas, though not as bad. You might enjoy this classic comedy routine: https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat
- dgb23 3y agoWith VSCode, you can use "// @ts-check", which essentially turns on a linter that assumes JS is strongly typed and respects any provided TS definitions and jsdoc comments. It's not the same as actual strong typing (like Python/Clojure), as it obviously doesn't affect the runtime, but it really helps.