Close menu

TypeScript vs JavaScript

TypeScript is a well established language that many developers opt for in preference to JavaScript, particularly for single page application projects.

What we find interesting though, is that the two are often compared as if they were mutually exclusive languages, and the question is usually 'which is better'. This is interesting, because this shouldn't be the case all.

What is TypeScript

TypeScript differs from JavaScript in 2 major ways:

  • It's a compiled language
  • It has optional static type checking

That may in itself sound significantly different to JavaScript, but it's important to remember what TypeScript actually is:

TypeScript is a superset of JavaScript

This means that TypeScript IS JavaScript, with additional features. In particular, the 2 mentioned above.

But that's not all TypeScript brings to the table, some other important and compelling additions, which effectively allow you to write JavaScript as if it were a strictly typed language, are:

  • Interfaces
  • Generics
  • Namespaces
  • Tuples
  • Enumerated types

That's not an exhaustive list, but it gives an idea as to why TypeScript is compelling, particularly if you have a background in similar languages like C# etc.

Given it's a superset, all valid JavaScript programs are automatically TypeScript programmes, without having to do anything other than give the files an extension of .ts.

Compiled To JS

Another important point; TypeScript compiles to JavaScript, which means that the end products are JS files.

This is of course, because other than Web Assembly, JavaScript is the only programming language that will run in a browser.

Type Checking Is Compile Time Only

TypeScript offers static type checking, which is great if you're more used to working with strictly-typed languages.

However, the transpiled JavaScript file that is produced does not include code to validate types - this is only done at compilation of the TypeScript file. If your final program must work with only particular types, then your program must perform the type checking just as you would when writing JavaScript.

When To Use TypeScript

When you are building:

  • Large applications
  • Private APIs
  • SPAs

TypeScript is a great fit for these because writing interfaces, and specifying types is really helpful when you have literally hundreds of files and even more functions.

Static typing is not only like writing JavaScript documentation as you develop, it also helps while coding because IDEs are able to use the type information to prompt the types you need to provide when calling functions etc. The larger your project, the harder it is to recall this type of stuff. TypeScript can be an immense help in this respect.

If you do use the wrong type somewhere in your code, compilation will fail which means catching bugs way before they hit production.

When To Use JavaScript

  • Small to medium applications
  • Building Public APIs
  • Website code
  • Leveraging the power of dynamic typing

For smaller projects, it's often more overhead (mentally as well as practically) to use the additional features that TypeScript provides. If your application is small enough to fit in a single file, or is a simple website script, then writing JavaScript means you don't have to configure anything or compile the code to get the results - and let's face it, waiting for code to compile sucks!

Bottom Line

TypeScript is a great tool for building large and complex web applications. But choosing it over JavaScript isn't about which is the better language; TypeScript IS JavaScript after all and you should pick the right tool for the job. TypeScript won't protect against incorrect types being supplied to any public APIs you expose in your code/application; personal preference should have very little to do with choosing between the two, but rather the scenario you developing in/for.

First published: 16/11/2020