DEV Community

Cover image for Type it out...
Clifton Beale
Clifton Beale

Posted on

Type it out...

What is TypeScript, why would you use it, and when would you use it?

Jump to section

TypeScript was one of those things that I was always reading/hearing about, but didn't look into myself yet as I was learning different things already. The buzz around TypeScript is real, which breeds a question: is it worth checking out?

What is TypeScript?

According to the TypeScript documentation, TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

What does that mean in lighter terms? Basically, you can cover your own back and prevent errors from popping up down the road where you would not expect them. The TypeScript tooling handles the type syntax as well as type inference - essentially, TypeScript pairs with and understands the JavaScript code being written and can infer types from syntax that lack it. You do not have to give a type everything

A type is simply a specification for a type of given data, I.E. string, number, boolean, or ect.

JavaScript logo changing to TypeScript Logo with an arrow pointing from left to right towards the TypeScript logo

Why use TypeScript?

As I mentioned before, one benefit of TypeScript is error prevention. This is always an important issue, especially if you are unsure if the scale that the codebase you are working with is going to expand/change. You might save your future self some headache by leveraging TypeScript.

Clip art image of someone with a headache at work at a computer

Readability of code is a huge deal that is touched on quite a bit. No one wants to have to read through a bunch of complex code with unique variable names and no comments. Well, to throw a little extra readability into the mix, TypeScript can help you understand what to expect with your codebase. With the types provided for you, it becomes easier to infer what the code is doing/performing.
Example:

interface Person {
name: string,
age: number
}
const person: Person = {
name: "Clif",
age: 24
}
Enter fullscreen mode Exit fullscreen mode

I created the interface of Person and declared that the new const variable of person was going to be of that type: Person. With that being said, the const variable has properties that are respectively a string and a number. (Any other data form will throw a type Error)

When would you use TypeScript?

Some would argue whether it is right or wrong to always use TypeScript, I don't think there is a right or wrong answer there. TypeScript is definitely a handy tool, but you can also get by without it. Essentially, I would say that it is a good idea to use TypeScript if you are unsure of the direction/scale of the work that you are doing. It is easier to start with it than to go back and add it down the road.

As mentioned earlier, there are many benefits with TypeScript when it comes to working with large teams/organizations, so there is a very good use case there. Readability, error prevention, and overall code quality are all strong factors that TypeScript can offer.

Team all putting their hands into the middle of a huddle

Benefits Recap

  1. Error Prevention

  2. Type Inference

  3. Code Quality/Scalability

  4. Code Readability

  5. Overall Productivity

Resources for learning TypeScript

Top comments (0)