Featured image of post Node.js- How to Use TypeScript

Node.js- How to Use TypeScript


Step 1: Install TypeScript

First things first, you need to install TypeScript.

Open your terminal and run:

1
npm install -g typescript

This installs TypeScript globally, so you can use it in any project like a wizard summoning spells.

If you want to install it locally (because global installs are so 2019), run:

1
npm install --save-dev typescript

This keeps your project dependencies clean and prevents conflicts with other projects.


Step 2: Initialize TypeScript in Your Project

Run the following command in your project directory:

1
tsconfig.json --init

This will generate a tsconfig.json file, which is basically TypeScript’s rulebook.

You can tweak it, but for now, the default settings should be fine.

If you want a quick setup, just use:

1
2
3
4
5
6
7
8
9
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "dist",
"rootDir": "src",
"strict": true
}
}

This makes sure your TypeScript compiles down to something Node.js understands while keeping your code well-organized.


Step 3: Write Your First TypeScript File

Create a src folder (if you haven’t already) and inside it, create a file called index.ts.

Now, let’s write some TypeScript magic:

1
2
3
4
5
6
7
const greet = (name: string): string => {
return `Hello, ${name}!

Welcome to the TypeScript side of Node.js.`;
};

console.log(greet("Developer"));

Notice how we’re actually specifying the type (string) for the name parameter and return type?

No more accidental undefined nightmares!


Step 4: Compile TypeScript to JavaScript

To convert TypeScript into something Node.js understands, run:

1
tsc

This compiles all .ts files in the project and outputs them into the dist directory (or whatever you specified in tsconfig.json).

Now, you can run your compiled code with:

1
node dist/index.js

And boom!

You just ran TypeScript in Node.js.

Feels good, right?


Step 5: Run TypeScript Directly With ts-node (Optional)

If you don’t want to keep compiling every time, install ts-node:

1
npm install -g ts-node

Then, run your TypeScript files directly:

1
ts-node src/index.ts

No compiling needed!

TypeScript on steroids.


Step 6: Use Type Definitions for Node.js

Node.js doesn’t know what TypeScript is by default, so you’ll need to install type definitions:

1
npm install --save-dev @types/node

This gives TypeScript the power to understand Node.js APIs, making your autocompletion dreams come true.


Step 7: Enjoy the Benefits

With TypeScript, you now get:

  • Type Safety: No more undefined is not a function nonsense.
  • Better Autocompletion: Your editor actually helps instead of mocking you.
  • Code Readability: Your future self will thank you.
  • More Confidence in Refactoring: Change things without fear!

Key Ideas

ConceptSummary
Install TypeScriptUse npm install -g typescript or npm install --save-dev typescript
Initialize ProjectRun tsconfig.json --init and configure tsconfig.json
Write TypeScriptCreate .ts files and use types for better safety
Compile to JSUse tsc to compile TypeScript files
Run Without CompilingUse ts-node to run TypeScript directly
Add Node.js Type DefinitionsInstall @types/node for better compatibility

References