Typescript — Why should one use it ?

Typescript — You definitely have heard this name, if you are a javascript developer. So if you are confused with what Typescript is? And…

Typescript — Why should one use it ?

Typescript — You definitely have heard this name, if you are a javascript developer. So if you are confused with what Typescript is? And why do we use it, when we already have javascript available which is fulfilling all our needs. I will try to answer some of the questions like this today.

What is Typescript?

Typescript is a superset of Javascript, which is designed to develop large Javascript applications. Being a superset of Javascript, existing Javascript programs are also valid Typescript programs, meaning you don’t have to worry about converting the whole existing Javascript code into Typescript at once. Instead if you wish to migrate your existing Javascript code to Typescript, you can do that gradually.

Typescript is a superset of Javascript

There are some major benefits of using Typescript, which includes, support for Classes and Modules, Type-checking, ES6 features support, Class library API definition, support for Javascript packaging, and a lot more.

A brief history

In 2010 Anders Hejlsberg (the founder of Typescript) and his team started working on Typescript at Microsoft and two years later in 2012 the very first version of Typescript was released to public (Typescript 0.8). Although the release of Typescript was praised by many people around the world, but still due to lack of support in major IDEs it was not majorly adopted by javascript community.

The first version of Typescript (Typescript 0.8) was released to public in 2012.

In 2013 another version of Typescript was released (Typescript 0.9) and many new features were added including support for generics. Also Microsoft added builtin support for Typescript in Visual Studio 2013.

One year later in 2014 at Microsoft Build Developers Conference, Typescript 1.0 was released. Later same year a new Typescript compiler was also introduced.

In 2016 Typescript 2.0 was released, adding many more new features to the language.

Typescript 2.1 was released in year 2017, and since then another 5 versions of Typescript are released. The current versions of Typescript is 2.6.

Why should one use it ?

Here are few features of Typescript which make its usage worth, and are a reason why a javascript developer should consider using the Typescript.

Using new features of ECMAScript:

TypeScript supports new ECMAScript standards and transpiles them to (older) ECMAScript targets of your choice (current targets are 3, 5 and 6 [a.k.a. 2015]). This means, you can use features of ES2015 and beyond, like modules, lambda functions, classes, the spread operator, de-structuring, today. It also adds type support of course, which is not part of any ECMAScript standard and may likely never be due to the interpreted nature instead of compiled nature of JavaScript. There are some other tools available to do this job as well. Like BabelJS. Typescript does this out of the box.

Static typing:

JavaScript is dynamically typed. This means JavaScript does not know what type a variable is until it is actually instantiated at run-time. TypeScript adds type support to JavaScript. Bugs that are caused by false assumptions of some variables being of a certain type can be completely eradicated if you play your cards right; how strict you type your code or if you type your code at all is up to you. TypeScript provides static typing through type annotations to enable type checking at compile time.

Type Inference:

TypeScript makes typing a bit easier and a lot less explicit by the usage of type inference. For example: var x = "hello" in TypeScript is the same as var x : string = "hello". The type is simply inferred from its use. Even it you don’t explicitly type the types, they are still there to save you from doing something which otherwise would result in a run-time error.

Better IDE support

The development experience with TypeScript is a great improvement over JavaScript. There is a wide range of IDEs that have excellent support for TypeScript, like Visual Studio & VS code, Atom, Sublime, and IntelliJ/WebStorm. These IDEs are well informed in real-time by the TypeScript compiler on its rich type information. This gives a couple of major advantages. For example, with TypeScript you can safely do refactoring like renames across your entire codebase.

Strict Null Checking

Errors like cannot read property 'x' of undefined are common in javascript programming. By using Typescript, you already avoid most of these kind of errors, since one cannot use a variable that is not known to the TypeScript compiler. But its still possible though to mistakenly utilize a variable that is set to undefined. By using --strictNullChecks flag in TypeScript compiler you can eliminate these kinds of errors all together.

Interoperability

TypeScript is closely related to JavaScript so it has great interoperability capabilities, but some extra work is required to work with JavaScript libraries in TypeScript.

These are just a few of the many advantages of using Typescript. You can discover many more by going through the following resources.

  1. https://www.typescriptlang.org/
  2. https://en.wikipedia.org/wiki/TypeScript
  3. https://www.upwork.com/hiring/community/the-advantages-of-typescript/
  4. https://medium.freecodecamp.org/when-should-i-use-typescript-311cb5fe801b
  5. https://stackoverflow.com/questions/12694530/what-is-typescript-and-why-would-i-use-it-in-place-of-javascript/35048303#35048303
Liked it ?

Read more