A beginner's guide to Typescript

What is Typescript?

In complex words, typescript is a syntactic superset of Javascript. In simple words, typescript is built on Javascript's syntax, but it allows developers to add static types. Static typing means that a variable's type can not be changed at any point in the program, unlike javascript. This helps to prevent a lot of bugs.

Pros and Cons of typescript

Here are some of the advantages and disadvantages of typescript:

Advantages:

  1. Typescript code is simpler to debug since type errors can be identified during compile time instead of runtime.

  2. Static typing makes your code easier to refactor and debug.

  3. Typescript allows for better code organizing which improves the readability of your code.

  4. Compatibiity across platforms and browsers: Any device, platform or browser that supports javascript also supports typescript, as far as it is compiled to javascript.

  5. Typescript saves development time, and thus allows developers to be more efficient.

Disadvantages:

  1. Error messages can be difficult to understand.

  2. Increased complexity.

  3. It can take longer time to write and to compile, which may not be suitable for larger projects.

  4. It is only used on the client side, unlike javascript which can be used on both client and server side.

However, since typescript will eventually be compiled into javascript, there's no real risk in choosing either of them.

Getting started with Typescript

To get started using typescript, you need to install it using npm. Assuming you already have node installed, run this command in your command line.

npm install -g typescript

It might bring a permissions error, in which case you have to type sudo in front of the command before running, as shown below:

sudo npm install -g typescript

After this, enter your system's password.

Running this command will allow you to install typescript globally on your machine, instead of installing it whenever you want to use it in a project. This way you can use tsc, which stands for typescript compiler, anywhere in your terminal.

Tsc helps you compile your typescript into javascript to be interpreted and run by the computer.

To confirm that the installation was successful, the following command should return the version of tsc you have installed.

tsc -v

You now have typescript installed, and you can start coding!