DEV Community

Cover image for 10 reasons why you should use Typescript
Anjan Shomooder
Anjan Shomooder

Posted on

10 reasons why you should use Typescript

Why should you use Typescript?

Simple. It solves your problem. Let's see.

  • Type checking: This is why typescript really exists. It adds type checking over javascript. It stops you to write stupid code.

Alt Text

Let's see an example:

const addNums = (a, b) = {
return a + b
}
Enter fullscreen mode Exit fullscreen mode

a and b should be numbers. But you stupidly pass strings.

addNums('1', '2')
Enter fullscreen mode Exit fullscreen mode

output:

'12'
Enter fullscreen mode Exit fullscreen mode

We don't want this to happen. It was a stupid example. But if you are working on a bigger project, this kind of bug can happen easily. You waste hours and hours to fix the code.

Alt Text

This is where typescript rescues us with type checking.

const addNums = (a: number, b: number) = {
return a + b
}
Enter fullscreen mode Exit fullscreen mode

Now if you pass string it will give you an error so that you can fix it.

  • Gives error at compile time instead of runtime: Instead of your code giving the error at code run time, typescript will throw you compile error.

  • Object oriented programming: Typescript allow you to use object-oriented programming concepts very easily. Like class, interface, inheritance, and so on.

  • Predictable Code: If you have type checking, then you know where to put what type of data. And which code will return which type of output. You can predict the code and it is very important

  • Great Intellisense: You will have far better IntelliSense with Typescript than JavaScript. If you don't believe me, try it out.

  • Write Next-generation code: Typescript allow you to write next generation code. It will compile down your code to older versions so that your code stays compatible with older browsers and javascript runtime.

  • Absolute import and alias: You can use absolute import and import aliases with typescript. They are super helpful.
    In case if you don't understand the absolute import.

Alt Text

  • Easy to understand: Typescript makes your code a lot easier to read and understand. Because you know what variable is which type and where to use what.

By the way, this blog is originally published on cules coding website.. I would be glad if you give it a visit.
Image description

  • Support with most libraries: Most of the third-party libraries has typescript support.

  • Great community: Typescript has a huge community. It is so much popular that most developer uses typescript over JavaScript.

and the list goes on and on........

There are a lot more things that are available on the typescript. But I believe they are enough to convince you to use typescript.

So,

Will you use Typescript after reading this blog?

Also if you use typescript what feature do you like most about it?

Please let me know in the comments section. I would love to hear from you.

Shameless Plug

I have made a video about how to build a carousel postcard with React, Material-UI, and Swiper.js.
If you are interested you can check the video.

You can also demo the application form here

Screenshot of Insta Carousel

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

If you have any questions, please comment down below.
You can reach out to me on social media as @thatanjan .
Stay safe. Goodbye.

About me

Why do I do what I do?

The Internet has revolutionized our life. I want to make the internet more beautiful and useful.

What do I do?

I ended up being a full-stack software engineer.

What can I do?

I can develop complex full-stack web applications like social media applications or e-commerce sites.

What have I done?

I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.
Alt Text

I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.

Want to work with me?

I am looking for a team where I can show my ambition and passion and produce great value for them.
Contact me through my email or any social media as @thatanjan . I would be happy to have a touch with you.

Contacts

Blogs you might want to read:

Videos might you might want to watch:




Discussion (21)

Collapse
christiankozalla profile image
Christian Kozalla

I find it astonishing that every article I see on DEV that recommends TypeScript has a comment section full counter-arguments, fierce discussions and sometimes hatred.

I mean, it's up to you whether you "should" use TypeScript. It gives you some benefits, but tradeoffs as well. Most certainly, it depends on the project whether TypeScript could be beneficial in the long run

Collapse
rammina profile image
Rammina • Edited on

Well, others simply are expressing their opinions and want to avoid misinformation from being spread.

Typescript's a little bit controversial because of its popularity and the number of devs "worshipping" it without considering its downsides.

And also I mean no offense, but there are so many similar posts about reasons to use Typescript with lack of quality and obvious self-promotion at the end. It's just too tired, and I don't blame the others for being annoyed/blunt towards content like these.

Collapse
christiankozalla profile image
Christian Kozalla

Thanks for explaining! That makes sense to me. Also, I agree on what you said about the typescript posts

It's not like typescript being the better Javascript in any aspect.

Collapse
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Not an official TC39 specification.

This is like saying Sass/Less/CSS preprocessors shouldn't be used because they're not mentioned in the official CSS specs. I don't see how that's relevant. TypeScript is an extension of JavaScript. It supports everything JavaScript does, but it extends the language statically. Types get stripped out at build time, and most other features get transpiled to spec-compliant JS.

It doesn't actually make your code easier to read. It creates more code.

I strongly disagree. Types require writing more code, yes, but that's not inherently a bad thing. Static type-checking means your code is easier to read because it enforces a strict contract for method signatures and variable types. You don't have to guess what values a particular function accepts. You also don't have to worry about devs on your team making silly typos.

If the architecture is made by a hype/resume driven engineer the code is essentially unreadable.

I don't know what this means, but based on your responses to others in this thread, it seems you associate TypeScript usage with being a "hype-driven engineer"? Lots of companies are moving their code bases to TypeScript for a good reason—not because it's a fad but because it's much more maintainable than untyped JS.

Typescript was introduced, in order to allure to C# and back end developers.

I don't know where you're getting this from, but I've never heard this. TypeScript was introduced to add static typing to JS.

Generics, tuples etc. are not part of JS specification. JS is NOT an OOP programming language.

Again, why does this matter? Higher-level languages and abstractions will always exist. TypeScript extends JavaScript with new features. You just have to learn how to use them. "It's not in the specification" sounds like an excuse to not learn TypeScript.

Usefulness of TS is for code completion when writing code but ajax calls might give you unpredictable data. Their isn't a runtime types feature.

Right, but TypeScript does not market itself as run-time type checking, so this point is moot. JavaScript also does not perform any runtime type-checking. It's always your responsibility to check that run-time data has the correct shape, regardless of whether you're using a statically or dynamically typed language.

Extra dependecy from npm and all the @types you will install as well. Extra transpilation as well if you also use babel.

Types get stripped out from production code. Most packages that are written in TS already come with types and don't require a separate @types installation.

You will eventually "fight" against the language trying to figure out why the linter is showing you cryptic errors.

Or, you'll learn to love TypeScript for helping you avoid problems that are incredibly frustrating to debug in vanilla JS.

Collapse
rammina profile image
Rammina

Another Typescript article again huh?

I wouldn't really always recommend Javascript developers to use Typescript. And there are some case scenarios and downsides that make me not use it in some cases..

aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

But why would I need to learn it anyway? Is TS native to the browser?

This is a flawed argument. TS gets transpiled to something that is native to the browser. So do lots of other languages, now that web assembly exists.

Depending on how reductionist a view you adopt, nothing is native to computers, and we should all be coding in assembly—nay, zeros and ones—to be as close to the hardware as possible. That way, the code we write can be directly executed on any target platform.

It sounds like you didn't really read my response. High-level abstractions will always exist. That's how programming works—just because a language needs to be compiled down to another language doesn't make it bad.

The amount of crappy code was amazing not to mention the bloat

Honestly, it sounds like you had a bad experience with TypeScript and are now generalizing this experience to TS and TS developers.

Collapse
joelbonetr profile image
JoelBonetR

I'm not joining this discussion anymore positioning myself in favour or against TS.

Just came to tell you as a Tech Lead on a big company that I never saw a bug in any environment from a project using JS related to any single thing that TS "come to solve". I've saw other stuff exploding, of course but none related to this. Mainly because those errors are prompted in dev time almost always.

In the other hand I saw few errors (luckily spotted by QA) related to the fake peace of mind that TS provides. Meaning that devs not checking its code to be valid for different types on a dynamic endpoint (content provider) and then getting weird behaviours on that.

TS is for DX (Developer experience) and we can agree or disagree, love or hate it, but at the end you need to know how the tech of your election is working behind the scenes, otherwise errors and bugs will show up sooner or later.

For me, every dev needs 2 things:

  • to get experience in testing your own features, keeping in mind all the possible use cases for your code and not only for the acceptance criteria of the task.
  • to understand the errors.

If you don't understand how the tech is working behind the scenes you'll not be able to interpret the errors properly, nor to spot all the use cases from your code and thus no magic tech will help you to became better.
Is TS the answer? Obviously not, it can help or harm depending on the situation and this requires in deep analysis from the current situation (the amount of devs available for the project, the current background of those devs, the requirements, the timeline with the deadlines and so on).
The tech lead (myself in my case) will decide according to this information if the project is suitable to be coded using TS or not.

There's no tech to rule them all, you need to analyse well what you need to do to find out the best possible tech stack and check if you have enough human resources with enough knowledge about this stack to go for it. This applies to any tech.

The conclusion is that if you are using TS like a mantra, you're probably missed the analysis part and thus you're probably wrong. Luckily people that act like this (tech fan boys) usually don't grow enough to burn other devs with it's own decisions.

Cheers

Collapse
zelcion profile image
Fábio M Jr

It seems that there are quite a few folks around who are quite negative around TypeScript, and I mostly get their point.

I still prefer to have the type system to stop me from making dumb mistakes, or helping me not needing to remember the exact interface of that class I made 3 Months ago. I'd go as far as saying it is crucial for having a successful big team of developers working around in the same code.

From experience, I can say that TypeScript enabled me to actually deliver my open source software, Meta-System mapikit.github.io/meta-system-docs/ . If it wasn't for using it, I'd be dead with the time lost trying to figure out messy interfaces, and validating huge JSON files.

Yes, you do end up writing more code, but that code is ensured to not randomly throw type errors at you.

Collapse
blanners profile image
Gauthier Blanpain

Just a friendly suggestion... numbering the 10 reasons list might make the post a bit easier to follow 👍

Collapse
thatanjan profile image
Anjan Shomooder Author

Hey, thank you for the suggestion.
I actually tried to do that. But in this platform if I try to create list with numbers and there are more content like images or text between any of the list, the list number breaks. I don't why that happens. But it works fine on my website.

Collapse
andreidascalu profile image
Andrei Dascalu

Assuming OOP is a good thing, JS has been capable of it for a while now natively.

Type safety at compile time is limited to your code (as long as you stay away from 'any'). Any native libraries (and by extension any interop with native libraries you pull in) is not covered. But you do get a false sense of security.

everyday.codes/javascript/7-really...

Collapse
declanpossnett profile image
Declan Possnett

@aliakakis Absolutely agree with this. TS just turns JS into something it's not.

Collapse
js_bits_bill profile image
JS Bits with Bill

Can't stand TS. Not very many people see the beauty in dynamic typing but it can be very powerful if you understand it.

Collapse
johnkazer profile image
John Kazer

I think on balance I would rather learn a natively typed language that compiles to JavaScript. Maybe Elm or ClojureScript

Collapse
charlesfries profile image
Charles Fries

TypeScript \u003e JavaScript :)

Collapse
joelbonetr profile image
JoelBonetR • Edited on

did you just mean \u003c? 😆

Collapse
pengeszikra profile image
Peter Vivo

against: already I can't able to add pipeline operator to TS code, but in js I use many times, because I write functional js code.

Collapse
christiankozalla profile image
Christian Kozalla

What is a hype/resume driven engineer?Do they all write the same code?