Getting started with Typescript

Getting started with Typescript

·

5 min read

Table of contents

No heading

No headings in the article.

Typescript is a popular programming language that is a super set of JavaScript. It is widely used in building large-scale applications and provides type checking at compile-time to catch errors before running the code. This article will provide an overview of how to use Typescript, including code snippets to help you get started. Before that, let's go over some of the reasons you should use Typescript:

Type checking

One of the biggest advantages of Typescript is its strong type system. This allows you to catch type errors at compile-time, rather than at runtime. This means that you can find and fix bugs in your code before it is even executed, saving you time and headaches.

For example, consider the following JavaScript code:

function add(a, b) {
  return a + b;
}

add("1", 2); // returns "12"

In this code, we have defined a function called add that takes in two arguments and returns their sum. However, if we call this function with arguments of the wrong type, the code will still run and return a value that may not be what we expect. In the case above, the function is called with a string and a number as arguments, but since the + operator concatenates strings in JavaScript, the function returns "12" instead of 3.

With Typescript, we can avoid this problem by specifying the types of our function arguments and return value:

function add(a: number, b: number): number {
  return a + b;
}

add("1", 2); // error: Argument of type '"1"' is not assignable to parameter of type 'number'.

In this code, we have added type annotations to our function signature, indicating that the add function takes in two numbers and returns a number. If we attempt to call the function with arguments of the wrong type, the Typescript compiler will give us an error, preventing the code from being executed.

Improved code organization

Typescript also provides features that can help you organize and structure your code better. For example, you can use interfaces to define the shape of an object, making it easier to ensure that your code is working with the correct data.

Here is an example of an interface in Typescript:

interface User {
  name: string;
  age: number;
}

This interface defines a User object that has a name property of type string and an age property of type number. We can then use this interface to define the type of a variable that holds a user object:

const user: User = {
  name: "John Doe",
  age: 30
};

In this code, we have defined a variable called user and indicated that it should be of type User. This means that the user variable must have a name property that is a string and an age property that is a number. If we try to assign an object to the user variable that does not match the shape defined in the User interface, the Typescript compiler will give us an error.

Better support for modern JavaScript features

Another reason to use Typescript is that it provides better support for modern JavaScript features. For example, Typescript has built-in support for features like async/await and the latest ECMAScript standard.

Now, let's get started.

Getting started with Typescript

To use Typescript, you will need to install it on your system. The easiest way to do this is through npm, the package manager for JavaScript.

npm install -g typescript

Once Typescript is installed, you can create a new project by creating a tsconfig.json file in the root of your project directory. This file specifies the compiler options for your Typescript project.

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist"
  }
}

In this example, we are targeting the ECMAScript 5 standard, using the CommonJS module system, and outputting the compiled JavaScript files to a dist directory.

Writing Typescript code

Once your project is set up, you can start writing Typescript code. To do this, create a new file with the .ts extension in your project directory. Here is an example of a simple Typescript file that defines a function to add two numbers together:

function add(a: number, b: number): number {
  return a + b;
}

In this code, we have defined a function called add that takes in two arguments, a and b, which are both numbers. The : number after each argument name indicates that the type of the argument is a number. The : number after the function name indicates that the return type of the function is a number.

Typescript's type system allows us to catch type errors at compile-time, rather than at runtime. For example, if we try to call the add function with arguments of the wrong type, the Typescript compiler will give us an error:

add("1", 2); // error: Argument of type '"1"' is not assignable to parameter of type 'number'.

In this code, we have attempted to call the add function with a string and a number as arguments, but since the function expects two numbers, the Typescript compiler will give us an error.

Compiling Typescript code

Once you have written your Typescript code, you will need to compile it to JavaScript before it can be run in a browser or Node.js environment. To do this, you can use the tsc command-line tool:

tsc

This will compile all of the Typescript files in your project, outputting the compiled JavaScript files to the directory specified in your tsconfig.json file. You can then run the generated JavaScript files in your desired environment.

In conclusion, Typescript is a powerful language that provides strong type checking and other features to help you build large-scale applications. By following the steps outlined in this article, you can get started with Typescript and begin using it in your own projects.