4 ms·
TS helps remove a whole class of bugs (type-related) by preventing you from building (or in this case, transpiling) code with known and potentially unintended t
by metaloha 3y ago
TS helps remove a whole class of bugs (type-related) by preventing you from building (or in this case, transpiling) code with known and potentially unintended type casting (eg. `let myName = "Joe"; myName = null;` This makes it impossible to push production code with these types of potential bugs.
But, you do add a transpilation step you might not have needed (unless you're writing ES6 code and are already using Babel or something to target ES5 browsers).
But, TypeScript is better for adding strong or static typing than anything else, IMHO, so if you need that feature, don't use anything else.
But, it can be harder to write good TS code because JS devs have long been taking advantage of the weak typing inherent to the language. It can be a challenge to change that mode of thinking and resist the temptation to use the `any` type in TS.
As to the list at the link, point 1 is a non-issue; if you're using advanced JS features that aren't supported by the browser, you already have a transpilation step. Point 2 is not a feature of JS, it's a side-effect. Static typing is awesome and (I would argue) necessary for advanced development. Strong typing is a bit of a double-edged sword, but I'd still rather have it than not. Point 3 is related to point 1 - if you already have a transpilation step for some reason and you want strong/static types, no reason not to jump to TS; it transpiles to JS same as CoffeeScript or Flow or JSX.
- metaloha 3y agoOh yeah, I'll mention JSDoc @type tags - hard pass, if you can. Comments should have NO EFFECT AT ALL WHATSOEVER on code. They're comments. Everything should be identical with and without them, they are 100% disposable. Moving any type of functionality into comments makes them brittle and now they can't be stripped out. The suggestion to add a `.d.ts` file is good - it allows people who do use TS to be able to use strong/static typing while using your library, without you needing to add any kind of type support within your library. Just make sure it stays updated ;)
- eyelidlessness 3y agoJSDoc types don’t have any effect on how the code runs. They just document the types they’re commenting on. They’re conceptually exactly the same as a .d.ts companion file, and both are just different ways to achieve roughly the same thing as having TypeScript types directly in the source.