How to use TypeScript on backend ?
Last Updated :
17 Jul, 2024
TypeScript, developed by Microsoft, enhances JavaScript by adding type safety, which significantly simplifies debugging and development. This guide will walk you through setting up a TypeScript backend with Node.js and Express, showing you how to leverage TypeScript’s robust features to prevent common runtime bugs and improve your code management.
Prerequisites:
- NodeJS: Basics of Node.js development.
- Express: Fundamental concepts of building web applications with Express.
- JavaScript: Familiarity with JavaScript syntax.
Project Setup and Module Installation:
Step 1: Run the following command in command prompt/bash/console to create a node project
npm init -y
Step 2: Adding the required dependencies using the following command.
npm i express
npm i typescript ts-node @types/node @types/express --save-dev
npm i -D @types/express
Notice the devDependency for typescript. Typescript is only required through the development process, In the end, It will be compiled to VanillaJS for runtime. Learn more about types of dependencies.
Note: @types libraries are development dependencies that provide TypeScript definitions for Express and Node.
Project Structure:

Step 3: Configure Typescript using the following command.
npx tsc --init
It will generate tsconfig.json where you can define parameters for typescript like which ECMAScript version to use (like ES3 (default), ES5, ES2015), enable strict type checking or not. Learn more about typescript configuration.
Step 4: Creating an express server, here we have named it server.ts
server.ts
// Importing module
import express from 'express';
const app = express();
const PORT:Number=3000;
// Handling GET / Request
app.get('/', (req, res) => {
res.send('Welcome to typescript backend!');
})
// Server setup
app.listen(PORT,() => {
console.log('The application is listening '
+ 'on port http://localhost:'+PORT);
})
Step 6: Configure package.json
Add the following line of code in package.json file, tsc command compiles typescript code to Vanilla JavaScript, while node server.js will take the generated Vanilla JavaScript file and start the server.
"scripts": {
"build": "tsc",
"start": " node server.js"
}
Step 7: Run the server using the following command.
npm run build
npm start
Output: Now open the http://localhost:3000 in any browser to see the server running.
Similar Reads
How to Use Fetch in TypeScript ? In TypeScript, fetching the data from any server or API using fetch() API is not the same as we used to do it in Plain or Vanilla JavaScript. Let us see how we can use the fetch API to fetch data from any server or API in TypeScript.NOTE: Before using this code, please make sure that you have jQuery
4 min read
How to use express in typescript ? In this article, we will see how to use Express in TypeScript. The TypeScript is a superset of JavaScript that provides type notation with the JavaScript, so we can handle our server, and very scalable for the future. Express is web framework helps to create server-side handling with the help of Nod
2 min read
How to Use NextJS in Typescript? TypeScript enhances Next.js applications by adding static type checking and improves developer experience through type safety. Integrating TypeScript into your Next.js project helps catch errors early and improves code maintainability. It offers even greater productivity and robustness to your web d
5 min read
How to use TypeScript with React? TypeScript enhances JavaScript by adding strict type definitions, making your code more robust and maintainable. ReactJS, a popular library for building user interfaces, pairs excellently with TypeScript to create clean, efficient, and scalable codebases. Combining TypeScript with React offers a pow
3 min read
How to add TypeScript in Next.js ? In this article, we will learn how to add TypeScript in Next.js.Why should we use TypeScript in our project? The fundamental concept of TypeScript is that it is type-strict, which means that each entity, be it a variable, a function, or an object has a definite data type. It allows minimum bugs in t
5 min read