Nodejs with TypeScript
- Nodejs must be installed on your local development machine.
- General understanding of linting, debugging, and deployment.
What is TypeScript?
In the above code, We tried to assign a value of string type to age and the typescript showed the error
Nodejs doesn't have built-in support for typescript. We will add the npm package typescript to add typescript compiler and @types/node to add type definition.
- Check the installed version of typescript and verify whether typescript is installed or not.
Note: Sometimes tsc --version command doesn't work on windows. You can try to run the command tsc.cmd --version to check the typescript version.
- Create a folder named nodejs-typescript and go to the current folder nodejs-typescript with CLI (Command Line Interface) as shown below.
- Initialize the npm (Node package manager) to create a nodejs project.
- Now, We will install the npm package @types/node and add a type checker for nodejs.
- We need typescript dependencies to write rich code for the local development machine only.
- We will initialize the configuration file for nodejs typescript.
- The above command will create a file named tsconfig.json inside the nodejs-typescript folder and add some code in JSON format.
Note: If the tsc --init command doesn't work, You can try to run the command tsc.cmd --init.
- We will replace the existing code in the tsconfig.json file with the following code:
In the above code, We are using properties
- rootDir: It specifies the directory where our source code exists. We are using the src folder to write the code.
- strict: It notifies the compiler whether to enable strict type-checking options or not. We set the property strict to boolean true.
- exclude: It specifies the file that we do not want to compile. We are excluding the "./node_modules" folder.
Express Server With a ".ts" Extension
Install the following npm packages:
- express: We will install express using npm install express --save to create server and routes.
- @types/express: We will install a type checker for express using npm install @types/express --save-dev.
Create a new folder src, add a new file index.ts, and add the following code to the index.ts file.
- In the above code, We are using:
- import keyword to include express module with nodejs typescript.
- listen() method of the express app to set the port 3000 on localhost.
- get() method to send a response on URL https://localhost:3000/.
Compiling TypeScript Files for Node.js
- We will run the npm command to install typescript for the current nodejs project.
- Now, We will execute the index.js file using the command
Execute TypeScript Source Files Directly With ts-node
- Hence, we use the npm package ts-node to execute the indes.ts file directly.
- We will run the following command to install ts-node.
- Now, We will run the command npx ts-node src/index.ts and it will produce the same output
TypeScript Integration With Third-party Node.js NPM Packages
- In the above code, We are using Express, Request, and Response interfaces to check the value type of app, req, and res respectively.
ESLint Package to Lint TypeScript Code
- We will run the following command to install the eslint package.
- We use @typescript-eslint/parser to parse the format for typescript code and @typescript-eslint/eslint-plugin to add rules for typescript specifications. We will run the following command.
- Now, create a file named .eslintrc.js inside the folder nodejs-typescript and add the following code:
- Add the lint property in the scripts object to package.json.
- Now, We will check the error for the index.ts file with the following command
Debugging TypeScript Source Files With Visual Studio Code
- We will create a folder .vscode inside nodejs-typescript.
- Create a launch.json file inside the ".vscode" folder and add the following code to the launch.json file
- Press ctrl + shift + D and press the arrow icon on the left of the Debug index.ts tab.
Deploying to Production
- We can deploy a typescript project for production. But the browser doesn't understand typescript.
TypeScript in the Node.js World
- TypeORM: It is an ORM (Object Relational Mapping) tool that supports multiple platforms.
- Prisma: It is also ORM (Object Relational Mapping) tool that helps to manage queries for the database and to migrate the database.
- AdonisJS: It is a backend framework that helps to build the backend without installing multiple npm packages
- FoalTs: It is also a well-documented nodejs framework to build complete web applications.
- We can directly execute a typescript file using npm packaged ts-node.
- We can integrate typescript with third-party npm packages by adding their respective typescript definition starting with @types.
- eslintrc.js file helps to add configuration, define rules, watch typescript files and detect issues.
- We use the command npx eslint . --ext ts to check errors only for typescript files
- launch.json file and ctrl + shift + D is used to debug typescript files.