TypeScript vs. JavaScript [Pros / cons]

Last updated Invalid date
TypeScript is arguably one of the best things to come to the JavaScript world helping developers comfortably create complex enterprise software without anxiety. In this post, we briefly break down the pros and cons of TypeScript over JavaScript.

In this article, we'll look at the pros and cons of TypeScript compared to JavaScript.

For an in-depth guide on when to use TypeScript, check out my existing When To Use TypeScript Guide.

TypeScript

TypeScript, a superset of JavaScript meant for crafting enterprise applications. Sometimes known as JavaScript that scales.

Pros

Type Safety

One of the reasons why I think myself and a lot of other developers gravitated towards JavaScript intially was not only because it's the language of the web with tons of really cool shit, but because we were annoyed with languages like Java.

When you're just getting started programming and programming things at a small scale, types can be a huge nuisance towards getting something to work.

When you move throughout your career, take on more responsibility and work on bigger complex-domain problems, you're quickly needing the ability to express yourself using objects.

And in JavaScript, sometimes you have to do some funny stuff to ensure that the object you're enountering at runtime is in-fact, the type and shape that you want it to be.

JavaScript-r's are used to solving these kinds of problems with Joi validations and Duck-typing.

TypeScript makes this a whole lot cleaner and easier. The advent of interfaces alone in a programming language personally saves me a lot of time.

Better expressability

Being able to express to your team members what forms and object can take and the things that it can and cannot do is such a primitive requirement of object-oriented programming and good software design.

With JavaScript, we rely a little too much on good class and variable naming in order to describe intent.

TypeScript allows you to use interfaces, abstract classes, types (+ lots of different flavours of types such as Union Types and Conditional Types) in order to represent your design intent and ensure that you and your team adheres to the contract for how things should be used.

You can opt in to types

One of the beautiful things about TypeScript is the ability to opt in to using types.

That means that you can be very specific with your typings by referencing other objects...

class User {
  public name: Username;
  public email: Email;

  constructor (name: Username, email: Email) {
    this.name = name;
    this.email = email;
  }
}

or less specific by using primitives...

class User {
  public name: string;
  public email: string;

  constructor (name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}

or you say screw it, and treat it like JavaScript...

class User {
  public name: any;
  public email: any;

  constructor (name: any, email: any) {
    this.name = name;
    this.email = email;
  }
}

Sometimes, especially when dealing with packages or legacy modules of code that is fully written in JavaScript, I'll refer it with the any type. This helps if you don't want to spend the time to convert the module to TypeScript or write a TypeScript Definition for.

Cons

Addition build step required

The build step required in order to compile TypeScript down to JavaScript is something that I haven't seen mentioned too often on other resources.

Generally speaking, with all the TypeScript saves you- this additional overhead isn't too bad of a trade-off, but it does mean you have to go about doing the following tasks in different ways:

Running a single TypeScript file

You'll want to do npm run install --save-dev ts-node and use ts-node to run a single file like:

ts-node script.ts

Debugging TypeScript code in Visual Studio Code

You'll want to setup a build task to compile your TypeScript first before executing your launch script in order to run the debugger on the resulting JavaScript.

You have to learn it

Like anything, you have to spend some time to learn it. The time investment is always a bummer.

But for me, I found that I was able to become productive with TypeScript within a couple of hours given that it closely resembled Java and that's what I learned programming with initially.

For newcomers to Object-Oriented Programming, it might be a little more challenging but ultimately, 95% of the difference is syntactical.



Discussion

Liked this? Sing it loud and proud 👨‍🎤.


1 Comment

Commenting has been disabled for now. To ask questions and discuss this post, join the community.

andrew
3 months ago

This was a very informative blog and I really enjoyed reading it. But I also have a few points regarding it to discuss with you.


Node.js is an open-source server-side JavaScript framework. There are pros and cons associated with using Node.js, however, before deciding whether or not to use it, you should evaluate the benefits and drawbacks of using this technology.


Pros of Node.JS


1. Robust technology stack

2. Offers an easy scalability


Cons of Node.JS


1. Asynchronous programming model

2. Immaturity of tooling


To check the full list of tips, visit - https://blog.alakmalak.com/pros-and-cons-of-node-js-web-application-development/?UTM-andrew-aug/


Stay in touch!



About the author

Khalil Stemmler,
Software Essentialist ⚡

I'm Khalil. I turn code-first developers into confident crafters without having to buy, read & digest hundreds of complex programming books. Using Software Essentialism, my philosophy of software design, I coach developers through boredom, imposter syndrome, and a lack of direction to master software design and architecture. Mastery though, is not the end goal. It is merely a step towards your Inward Pull.



View more in Web Development



You may also enjoy...

A few more related articles

Over $85 billion spent on fixing bad code [bootcamps, junior devs, JavaScript, and software design principles]
More and more money is being spent by companies on maintaining bad JavaScript code. Here's where I think our industry needs to cha...
How to Build a Related Posts Component with Gatsby.js
Here's how to build a "Related Posts" component for your Gatsby.js site.
When To Use TypeScript - A Detailed Guide Through Common Scenarios
Strap yourself in. In this guide, we compare when it's absolutely vital to be using TypeScript, the strictly-typed programming lan...
Cleaner Code with Async / Await Tutorial
Asynchronous programming in JavaScript used to be difficult to write clean code with. Today we have options. You can write asynchr...

Want to be notified when new content comes out?

Join 15000+ other Software Essentialists learning how to master The Essentials of software design and architecture.

Get updates