TypeScript Introduction

TypeScript Introduction

·

3 min read

TypeScript is a superset of JavaScript. It means that it has all the features of JavaScript and adds more on top of them. TypeScript’s features like static typing and compile-time checks, including all the features of ECMAScript (a JavaScript standard), give numerous advantages while developing software.

Microsoft released TypeScript to the public in 2012. It is currently managed by Anders Hejlsberg, the creator of Turbo Pascal, C#, and Delphi. The project is open-source and hosted on GitHub.

TypeScript is transpiled into JavaScript so that the produced code can provide a polyfill for browsers that do not support the latest ECMAScript features. It enforces static typing, allowing one to identify issues in constructs during transpilation.

Why TypeScript?

TypeScript lets you transpile it into JavaScript for all browsers, even the ones that do not support the latest ECMAScript features. It provides a polyfill that is specific to the version of JavaScript supported by that particular browser. This allows you to use the same modern TypeScript syntax while also ensuring backward compatibility.

TypeScript also lets you use all the libraries and frameworks you already know. You don’t have to learn the equivalents, given its interoperability. And, TypeScript uses NPM (Node Package Manager). This means you don’t have to learn a new tool to be able to use TypeScript.

It brings static typing to the code base, making the code more readable, and easier to refactor. It is easier to find a particular member and modify all of its occurrences. It also saves time in testing by reducing the required unit tests as the transpiler does the work of checking for errors with static types, undefined or null values, etc.

Getting Started

Here’s my effort to get you to install and write your first few lines of code in TypeScript if you haven’t already. Make sure you have npm installed on your machine. It comes with installing NodeJS.

To install TypeScript on your machine, use the following command in your terminal or command prompt:

npm install -g typescript

To make sure it installed TypeScript on your machine, run the following code to check for the version of TypeScript installed on your machine.

tsc -v

Now, open up a folder in VSCode (or your favorite code editor) and create a TypeScript file. The extension of a TypeScript file is (.ts). Let’s write some code!

let myName: string = "Charan";
console.log(`Hello, ${name}!`);

Save the file with a name; I’m going to call it index.js and run the following command to transpile it.

tsc index.ts

This will generate a corresponding index.js file with the code transpiled into JavaScript, which in this case would look like the one below.

let myName = "Charan";
console.log(`Hello, ${name}!`);

You can run this code in your terminal/command prompt with the following command:

node index.js

Since TypeScript is a superset of JavaScript, it only makes sense to learn JavaScript first and then dive into TypeScript. And most resources, if not all, already assume that you understand JavaScript. Speaking of resources, you can find plenty of tutorials on YouTube from channels like freeCodeCamp, and other popular ones. But I’m a person who prefers reading over watching. If you’re like me, you can try https://learntypescript.dev/

If you have any other resources that you think would be a great addition, feel free to add them in the comments section.

Conclusion

TypeScript has surely seen a tremendous increase in its adoption by companies like Slack, Medium, Canva, and more. I've seen several organizations like rocket.chat, Sugar Labs, and more in GSoC (Google Summer of Code) mention TypeScript for their open-source projects. With that being said, whether you're a student with a good understanding of JavaScript or a seasoned JavaScript developer, TypeScript is a great tool to add to your arsenal.

Did you find this article valuable?

Support Charan by becoming a sponsor. Any amount is appreciated!