How to run typescript files from the command line.

Learn how to run TypeScript code from the command line.

TypeScript is a superset of JavaScript that includes features like type safety and static checking.

Browsers understand JavaScript files, so TypeScript code must be compiled and converted to JavaScript to run in a browser.

Compiling TypeScript requires a TypeScript compiler on our machine.

The following steps are required to run TypeScript:

Typescript code -> compile to javascript -> Run javascript

There are multiple ways to do this.

Using the tsc command: Executing TypeScript files from the terminal

tsc is a TypeScript compiler that comes with a TypeScript installation.

First, check if Node.js is installed using the following command:

A:\work>node --version
v14.17.0

A:\work>npm --version
7.11.1

This should output valid version information. If the version is not found, please install Node.js, which is not covered in this tutorial.

Next, install the TypeScript library using the npm command.

npm install -g typescript

With TypeScript installed, the tsc command becomes available.

Now, check if TypeScript is installed using the following command.

A:\work\>tsc --version
Version 4.1.3

Create a TypeScript file using any text editor of your choice.

For example, let’s create a TypeScript file with the extension .ts.

Here’s an example class file named Employee.ts:

class Employee {
  id: number;
  name: string;

  constructor(id: number, name: string) {
    this.id = id;
    this.name = name;
  }
  printEmployeeInfo(): void {
    console.log("Id = " + this.id + ", name = " + this.name);
  }
}

let employee = new Employee(11, "John");
employee.printEmployeeInfo();

To run the TypeScript file, use the tsc command followed by the filename,

Following are steps to generate a javascript

  • Compile typescript to the javascript file

Run the below command to generate a javascript file.

This will transpile the TypeScript file with the default TypeScript configuration and generate a JavaScript file Employee.js.

tsc Employee.ts
  • Execute javascipript file

To execute the JavaScript file, use the node command:

node Employee.js

You will see the following output:

"Id = 11, name = John"

The above two commands can be combined using the pipe (| in Windows, && in Unix or Mac) symbol and run with a single line as given below:

tsc Employee.ts |  node Employee.js
tsc Employee.ts &&  node Employee.js

Both commands yield the same output.

Using ts-node to run TypeScript files from the command line

ts-node is a TypeScript node library.

To install ts-node, use the following command.

npm install -g ts-node

After the installation of ts-node, the ts-node command becomes available to run TypeScript files.

Here is the command to run a TypeScript file.

ts-node Employee.ts

Output:

"Id = 11, name = John"

Watching changes with ts-node:

To transpile TypeScript files in watch mode, use the following command:

ts-node --watch Employee.ts

What’s the difference between tsc and ts-node?

Both tsc (TypeScript compiler) and ts-node are used to compile and transpile TypeScript into JavaScript.

tsctsc-node
Generates JavaScript files as per tsconfig configuration.Compiles files from the given order.
entry file or main file with step-by-step flow of importing required files.Tsc is inbuilt from typescript npm package. It is a wrapper npm library for Nodejs executable typescript
Used in production.Used in development for watching changes
Best performance.Slower compared with tsc.