TypeScript with Node.js: A Simple Guide

TypeScript with Node.js: A Simple Guide

1. JavaScript vs TypeScript: Which is Better?

JavaScript and TypeScript are both popular for web and backend development. TypeScript is an improved version of JavaScript with extra features like:

  • Static Typing: Finds errors early while coding.
  • Better Code Maintenance: Helpful for big projects.
  • Supports Object-Oriented Programming: Uses classes, modules, and interfaces.
  • Better Tool Support: Provides IntelliSense and auto-completion.

If you are working on small projects or quick scripts, JavaScript is fine. But for big projects, TypeScript is a better choice.

2. JavaScript vs TypeScript with Node.js: Which is Better?

Using JavaScript with Node.js

✅ Easy and fast development ✅ Supports many libraries and packages ✅ No extra setup needed

❌ Difficult to manage in big projects ❌ No type checking, which can cause more errors

Using TypeScript with Node.js

✅ More readable and maintainable code ✅ Finds errors before running the code ✅ Best for large applications

❌ Takes time to learn if you are new to JavaScript ❌ Needs an extra step to convert TypeScript to JavaScript

For small projects, JavaScript is okay. But for scalable and maintainable applications, TypeScript is the best choice.

3. TypeScript Features That Improve Code (Not Available in JavaScript)

TypeScript has many features that JavaScript does not have, which make the code better and safer:

  • Static Typing: You can define types for variables and functions to avoid errors.
  • Interfaces: Helps define the structure of objects.
  • Generics: Makes the code reusable and flexible.
  • Enums: Allows defining fixed values.
  • Access Modifiers (public, private, protected): Controls access to class properties.
  • Tuple Types: Defines fixed-length arrays.
  • Optional Chaining & Nullish Coalescing: ?. and ?? operators help manage missing values safely.

These features make the code easier to read, manage, and debug.

4. Node.js and TypeScript Versions

Latest stable versions:

  • Node.js: v20+ (LTS recommended)
  • TypeScript: v5+

You can check your installed versions using:

node -v
        
tsc -v
        

5. Setting Up TypeScript in a Node.js Project (Step-by-Step Guide)

Follow these steps to use TypeScript in Node.js:

Step 1: Install Node.js

If you don’t have Node.js installed, download it from Node.js official site.

Step 2: Create a New Project

mkdir my-typescript-app
cd my-typescript-app
npm init -y
        

Step 3: Install TypeScript

npm install -D typescript ts-node @types/node
        

Step 4: Create a TypeScript Configuration File

Create a tsconfig.json file and add this content:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "CommonJS",
    "outDir": "dist",
    "rootDir": "src",
    "strict": true
  }
}
        

Step 5: Create a Source Folder

mkdir src
        

Create a src/index.ts file and write this code:

const greet = (name: string): string => {
    return `Hello, ${name}!`;
};

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

Step 6: Run the Code

npx ts-node src/index.ts
        

To convert TypeScript to JavaScript:

npx tsc
        

Your compiled JavaScript files will be in the dist folder.

6. Creating a Simple TypeScript Project with Node.js

If you want to set up an Express server using TypeScript:

npm install express @types/express
        

Create a src/server.ts file:

import express from 'express';

const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
    res.send('Hello, TypeScript with Node.js!');
});

app.listen(PORT, () => {
    console.log(`Server running on http://localhost:${PORT}`);
});
        

Run the server:

npx ts-node src/server.ts
        

7. Best Practices and Conclusion

  • Always use strict mode: "strict": true in tsconfig.json
  • Use interfaces for better type safety
  • Avoid using any type
  • Prefer async/await over callbacks
  • Organize your project properly (controllers, services, routes, etc.)

Conclusion

If you want type safety and easy maintenance, TypeScript with Node.js is a great choice. JavaScript works for small projects, but for long-term development, TypeScript is more beneficial. 😊

To view or add a comment, sign in

More articles by Gourav Gangwar

Others also viewed

Explore content categories