DEV Community

Discussion on: Why You Should Use Typescript Over Javascript?

tqbit profile image
tq-bit • Edited on

Note that you can also use jsdoc for ts-like typing.

VSCode also has this convenient builtin feature that allows you to infer types from those comments. You just have to activate this one:

"javascript.implicitProjectConfig.checkJs": true
Enter fullscreen mode Exit fullscreen mode

I get why people prefer TS over JS Really. I just also noticed TS types clutter the code quite a bit (especially if you're used to dynamic typing) and require an additional compiling step. I seldom see this considered in posts about the two languages.

shreyanshsheth profile image
Shreyansh sheth Author

Yes, JSDoc is also a nice alternative if you don't want to use typescript .basically they both achieve the same things differently. ​
And One more compiling step does not affect anything at all because in the end, you are getting what you need.

The main thing is it boils down to personal preference that how you prefer writing your code.

jfbrennan profile image
Jordan Brennan • Edited on

One more compiling step does not affect anything at all

But it does. It adds dependencies to your project. Dependencies have to been installed, updated, and peer-dependency issues are major pain. It also increases build time. It also requires additional configuration, which is often a pain to work with. As the complexity of the project grows you have cases that conflict with TS and you have to start making tradeoffs between doing what needs to be done vs. making the compiler happy. You also have to remember that the compilation step is a bit of false sense of security, i.e. programmers often joke "But it compiled!" when the app fails at runtime.

Like many others, I've found the cost of writing non-standard dependency- and config-laden code that still doesn't ensure runtime safety is too high to justify its small benefits. A good IDE does all the inference and code-complete you need, the rest is noise imo.

anuragvohraec profile image
Anurag Vohra

jsdoc is way cumbersome to use, as compared to typescript (especially Typescript infering many things on its own, where as jsdoc is bit verbose).
Further more, Typescript provide compilation, and future javascript now.

tqbit profile image

I get your point. You've got to learn it, but so would you have to get used to Typescript syntax. I didn't really find it cumbersome. And about verbosity - that's what JSDoc is there for I guess. But instead of having all the types inside your code, you can keep them separate and even enrich them with intel that eventually ends up in your technical documentation. I'm sure there are ways around this as well, but then again why use TS next to JSDoc for automated doc creation?

kalashin1 profile image
Kinanee Samson • Edited on

TypeScript was built for application development, i Write and ship out products built with TypeScript faster than those built with JavaScript.

patarapolw profile image
Pacharapol Withayasakpunt

Yes, I do use JSDoc, along with // @ts-check, if I had to use JavaScript.

However, not only that it is harder to declare complex types, but also that IntelliSense is still less smart, even with ts-check on.

tqbit profile image

Your comment got me curious on complex types - I haven't used JSDoc trying to declare such yet. Can you please explain this a bit further?
I couldn't find a lot of good examples in the ts docs, neither on the www.

Thread Thread
patarapolw profile image
Pacharapol Withayasakpunt • Edited on

As far as I know, you can

  • Use /** @type {} */ to specify types for a variable
  • Use /** @type {} */ (x) to force a variable to type
  • Use /** @typedef {} */ to declare a type in advance

But still,

  • I don't know how to declare generics.
  • I don't know how to export types of JSDoc from a file.

BTW, much of typing works inside /** @type {} */'s bracelets. You can even use import().Class and use TypeScript interface syntax inside them.