DEV Community

Discussion on: Why You Should Use Typescript Over Javascript?

Collapse
 
tqbit profile image
tq-bit • Edited

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

jsdoc.app/tags-typedef.html

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.

Collapse
 
shreyanshsheth profile image
Shreyansh sheth

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.

Collapse
 
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.

Collapse
 
tqbit profile image
tq-bit

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?

Collapse
 
kalashin1 profile image
Kinanee Samson • Edited

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

Collapse
 
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.

Collapse
 
tqbit profile image
tq-bit

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

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.