DEV Community

Cover image for Why TypeScript is Better Than JavaScript: Top Benefits for Modern Web Development

Why TypeScript is Better Than JavaScript: Top Benefits for Modern Web Development

Akash on August 08, 2024

For a web developer, choosing the right programming language or tool for a specific project is paramount. The lately going-on increase in TypeScrip...
Collapse
 
miketalbot profile image
Mike Talbot ⭐

I agree that Better Refactoring is true for interface redefinition, that's a very good point.

I find that I don't really agree with "Improved IDE Integration" - I get highly accurate IntelliSense using JSDoc and the smarts of WebStorm, I never have to remember anything these days :)

Collapse
 
codeakash profile image
Akash

Thanks for your input! You’re right—JSDoc with WebStorm offers great IntelliSense. TypeScript’s advantage, in my view, is in adding compile-time type checking, which can catch errors earlier. But it definitely depends on personal preference and project needs. Appreciate your perspective!

Collapse
 
samuraiseoul profile image
Sophie The Lionhart

I don't think this is true? JSDoc is powerful but as I understand nowhere near as powerful as Typescript. It barely supports the idea of abstract classes from what I could tell when I tried it a few weeks ago and declaring re-usable types for objects in the way we make interfaces in TS was a pain at best. Those things give you better IDE integration as they tell your IDE better how things are supposed to work. Plus if you're already writing all that boiler plate, why not use TS anyways since its more powerful? The only issue I find with it is that compiling for frontend is still a pain in the ass and the fact that it doesn't work without build tools well or if it does there is a serious lack of easy to find documentation for it. Even then though, if you're doing most modern frontend web dev, you probably have a frontend build pipeline anyways so it seems like a moot point to point at that as the barrier too.

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Abstract classes are just a construct used to structure something. I don't use them; I use interfaces because they are more handy. Interfaces work fine in JSDoc. I don't know about abstract classes.

Thread Thread
 
samuraiseoul profile image
Sophie The Lionhart

Abstract classes and interfaces should be very different. A good class heirarchy should have both interfaces and abstract classes mixed. There are things that abstract classes denote and functionalities that they provide that can not be leveraged by an interface unless they are using the word interface as a misnomer.

Collapse
 
codeakash profile image
Akash

Thanks for your detailed response, Sophie! You’re right—TypeScript does offer more powerful features like abstract classes and interfaces, which definitely enhance IDE support. I also get what you’re saying about the build process; it can be a bit of a pain. But as you mentioned, with most modern projects having a build pipeline, adding TypeScript might not be as tough. Appreciate you sharing your thoughts!

Collapse
 
beelalaminkhan profile image
Bilal Khan

Typescript is more likely a IDE extension, and when used with React the code really becomes messy. And doesn’t provide a great developer experience.

I wish there was a more better way to write React with Typescript.

Collapse
 
pengeszikra profile image
Peter Vivo

I wrote a two React state handling npm module. One of them is typescript:
react-state-factory
and the next one is jsDoc state handling without react-saga support. So that is much more lightweight.
jsdoc-duck
Each of these library just for one prupose: give state and actions type and get a useReducer result with typesafe actions.

Of course does not use this lib any one. ( expect me ) But lets take a look that is give a real lightweight midcomplex state handling for react and each code under 100 LOC.

Collapse
 
codeakash profile image
Akash

Thanks for your input! I get what you mean—TypeScript can feel cumbersome with React. It’s all about balancing structure and simplicity. Hopefully, future tools will make this smoother. Appreciate your perspective!

Collapse
 
lwhiteley profile image
Layton Whiteley • Edited

Curious question:

What about typescript+ react is cumbersome ?

I find it to be a better interface than before with using react proptypes. It's much more expressive and also shareable.

You can write component types and extend them for higher level components.

Another note is that typescript propagates types from lower level to upper level..

Once used correctly the types propagate and get out of your way..it just provides intelligence for the consumers.

I also see a lot of code where types are imported when they are not needed.. retyping things when typescript can provide inferences.

This can indeed be cumbersome but taking advantage of inferring types can reduce this pain

Thread Thread
 
codeakash profile image
Akash

Thanks for the thoughtful question! I agree—when used well, TypeScript can really enhance the React experience. It offers a more expressive, shareable interface compared to PropTypes, and the way it propagates types up the component tree is powerful.

I think the "cumbersome" feeling often comes from over-typing or not fully leveraging TypeScript’s type inference, which can lead to unnecessary complexity. But once you get the hang of it and let TypeScript do its thing, it really does streamline development and boosts productivity.

Appreciate your insights on this!