This page looks best with JavaScript enabled

What is Typescript and why should I care?

 ·   ·  ☕ 4 min read

I had been fascinated by Typescript and have been going back and forth on making it my primary programming language. In a few posts and with as much less information as possible, let’s see why anyone should bother with Typescript.

What is Typescript?

Typescript is a typed Javascript.

No, I am kidding - it is more than that. Typescript is not only strongly typed language that acts as a super-set of Javascript, but also is compiled and object oriented language. What distinguishes Typescript is not the language alone but the tooling that surrounds the language.

Typescript was invented and popularised by the good folks at Microsoft. It is in active development and continues to add amazing features with each release.

The Typescript ecosystem makes you welcome as a Javascript developer, does not impose itself but gives you really powerful tools if you want dive right in. Your development is going to be more standardized, faster, and your code is going to be friendly with everyone in the team.

I see a great value for Typescript in a team setting or for components and libraries that are used by everyone and their dogs.

Why another language?

You should use Typescript because it is and is not another language at the same time.

Strong typing may seem daunting at first but the amount of errors it catches by itself and the ease of development(?) more than makes up for the initial hiccups and the extra typing.

There are of course a host of features that make Typescript invaluable and my objective here is not to cover them all. Key features include -

  • Compiled language - catch more errors at compile and not at runtime
  • Good support for object oriented features (at least during development / compilation)
  • Runs seamlessly across environments - web, operating systems and devices
  • Typing everything can be the most beautiful form of self-documenting everything
  • Easier to code!

But, I am a Javascript fanatic

Typescript is not a new language housed in an island and its own ecosystem. It coexists with Javascript.

  • Typescript is Javascript++. Typescript gets compiled to Javascript for execution when required
  • You can use both Javascript and other Typescript libraries within Typescript. You can use Typescript libraries within Javascript too
  • Migrations can be incremental. You can use inferred typing and get started on Typescript with existing code

Get started

Getting started with Typescript is easy. Let us setup the development environment first.

1
2
npm install -g typescript

Then, create a new file “hello.ts”.

1
2
3
// hello.ts

console.log("hello world");

If you now do -

1
tsc hello.ts

.. you will get a hello.js file. This is a JS compiled version from your Typescript file.

Since we did not quite use any Typescript specific stuff, our JS file is same as TS file.

1
console.log("hello world");

You can run the JS file to the same end result as a normal JS file.

Alternatively, you can create the TS file and simply execute two commands in one go -

1
tsc class1.ts | node class1.js

Or, use a better utility built to the purpose.

Install ts-node

1
npm i -g ts-node

Now, you can use nodemon to watch for changes in your Typescript file, generate Javascript when required and execute the Javascript - in one seamless way.

1
nodemon hello.ts

We do not see the intermittent file “hello.js” using this method but it is recommended for development purposes only.

Write a better typescript example

Create a new file sum.ts.

1
2
3
4
5
6
7
function getSum(x: number, y: number): number {
  return x + y;
}

const sum: number = getSum(1, 2);
console.log("sum: ", sum);
// 3

You may observe that when you type getSum(, the autocomplete will suggest the type or arguments and the return. This auto-suggestion does not seem all that important, but is super useful when you are working in teams or have to work with third party libraries.

Next, try to do -

1
2
3
const sum: number = getSum("a", 2);
// TSError: ⨯ Unable to compile TypeScript:
// class1.ts:5:28 - error TS2345: Argument of type '"a"' is not assignable to parameter of type 'number'

You see a couple of significant things -

  1. Editors like VSCode catch the error - no compilation or execution required. The ‘a’ argument is underlined with red squiggly in the hope that you correct the error
  2. Even if you ignore the editor error, Typescript does not compile the file but throws the error outlined above

Also see

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things