How to run typescript files from the command line.
- Admin
- Sep 20, 2023
- Typescript
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 compile and convert to javascript to run in a browser.
Compile typescript requires a typescript compiler in our machine.
The following steps are required to run it typescript.
Typescript code -> compile to javascript -> Run javascript
There are multiple ways we can do this.
tsc command: Execute typescript files from the terminal
tsc is a typescript compiler that comes with a typescript installation.
First check node is installed or not using the below command.
A:\work>node --version
v14.17.0
A:\work>npm --version
7.11.1
This outputs valid version information. If the version is not found, please install nodejs, which is not covered in this tutorial.
Next, Install the typescript library using the npm command.
npm install -g typescript
typescript installed, Now the tsc command is available to work
Now, Check if typescript is installed or not using the below command
A:\work\>tsc --version
Version 4.1.3
Create a typescript using any editor of your choice.
In addition, a typescript file with the extension ‘ts’ or ‘TSX’ was created. Here is my class-created typescript file - 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();
Run the typescript file using the tsc command below.
To execute a typescript file.
Following are steps
- Compile typescript to the javascript file Run the below command to generate a javascript file.
tsc command does transpile typescript file with default typescript configuration and generates javascript file Employee.js
tsc Employee.ts
- Execute javascipript file
use the node command to run the javascript file
node Employee.js
and running Employ
"Id = 11, name = John"
The above two commands can be combined using the symbol(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
It also gives the same output.
ts-node to run typescript files from the command line
ts-node is a typescript node library
npm install -g ts-node
After the installation of ts-node is done. a ts-node command is available to run the typescript file.
Here is a command
ts-node Employee.ts
Output:
"Id = 11, name = John"
Watching changes with ts-node
transpile ts files in watch mode
ts-node --watch Employee.ts
What’s the difference between tsc and ts-node?
Both tsc(TypeScript compiler) and ts-node uses to compile and transpile typescript into javascript.
tsc | tsc-node |
---|---|
tsc command generates javascript files as per tsconfig configuration.. | ts-node 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 |
Performance is best | It is slower compared with tsc. |