Let’s bootstrap a simple Typescript Node.js application locally on our machines

Step 1 - Install tsc/typescript globally

npm install -g typescript

Step 2 - Initialize an empty Node.js project with typescript

mkdir node-app
cd node-app
npm init -y
npx tsc --init

These commands should initialize two files in your project

image.png

Step 3 - Create a a.ts file

const x: number = 1;
console.log(x);

Step 4 - Compile the ts file to js file

tsc -b

Step 5 - Explore the newly generated index.js file

image.png

Notice how there is no typescript code in the javascript file. It’s a plain old js file with no types

Step 7 - Delete a.js

Step 6 - Try assigning x to a string

Make sure you convert the const to let

let x: number = 1;
x = "harkirat"
console.log(x);

Step 7 - Try compiling the code again

tsc -b