Learn Node.js unit testing with Jest, using mocking, snapshots, and best practices to ensure reliable and efficient application performance.
Typescript is an open-source programming language by Microsoft. Typescript is a statically typed language like C, C++, and Java. And Typescript makes javascript more capable. However, Javascript is a dynamically typed language i.e Checking the type of variable, object, function, etc is not mandatory. But We can enable static typing and perform type checks using TypeScript.
age and assign a value to the age variable. Consider the following example without TypeScriptlet age = 25string to the variable age. We can check the type of age using the typeof property as shown below:console.log(typeof age)age and perform a type check as shown below:let age: number = '25'
Type 'string' is not assignable to the type 'number'. Hence, TypeScript is checking the type of the age variable before executing the code.number to the variable age and fix the error as shown belowlet age: number = 25node-js-with-typescript using the following command:mkdir node-js-with-typescriptnode-js-with-typescript and initialize a node project with the following commands:cd node-js-with-typescript
npm init -ynode-js-with-typescript folder in Visual Studio Code.code .Note: You can use any code editor to work with the nodejs project. However, It is recommended to use Visual Studio Code for this article.
node-js-with-typescript/
└─ package.jsontypescript to the node js application using the following command:npm install -g typescripttypescript as shown below:tsc --versiontsc --inittsconfig.json inside the folder node-js-with-typescripttsconfig.json with the following code block:{
"compilerOptions": {
"outDir": "./dist",
/* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
/* Modules */
"module": "commonjs",
/* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true,
/* Type Checking */
"strict": true
}
}npm install typescript --save-devsrc inside the folder node-js-with-typescript and a file index.ts inside the folder src.node-js-with-typescript/
├─ src
| ├─ index.ts
└─ package.jsonindex.ts file:function addTwoNumbers(x: number, y: number) {
let z = x + y
return z
}npx tscdist and a file index.js inside the folder dist. Now, We will run the following command:node dist/index.jsts-node For Executing TypeScript Filests-node to execute typescript files directlyts-node as a dev dependency with the following command:npm install ts-node --save-devindex.ts file directlynpx ts-node src/index.tsstart property to scripts object in package.json as shown below:"start": "ts-node src/index.ts"npm run start to execute typescript files.We can use typescript with any npm package. Here, We will learn to use typescript with Express JS.
mkdir node-js-express-with-typescript
cd node-js-express-with-typescriptnpm init -ypackage.json inside the folder node-js-express-with-typescriptpackage.json fileexpress with the following command:npm install express --savetypescript to use TypeScript in Node JS Application, @types/node to add type support for Node JS, @types/express to add type support for Express JS, ts-node to execute typescript files and nodemon to restart the server if any changes occur to the file.npm install typescript @types/node @types/express ts-node nodemon --save-devnode-js-express-with-typescript using the command:code .server.ts inside the folder node-js-express-with-typescript and Add a start property to the scripts object in the package.json file as shown below"start": "nodemon src/server.ts",server.ts file:import express, { Request, Response, Application } from 'express'
// Initiate the express
const initExpress: Application = express()
initExpress.get('/', (req: Request, res: Response) => {
res.send(
`<h1>Hey! You successfully set up the express server with typescript.</h1>`,
)
})
// Listening to the express server at Port 9000
const PORT = 9000
initExpress.listen(PORT, () => {
console.log(`I am listening at ${PORT}`)
})Application is the type for initExpress, Request is the type for req and Response is the type for res.build property to scripts object in the package.json file as shown below:"build": "npx tsc && node dist/index.js"npm run build to compile the typescript files and execute the javascript files.npm install typescript --save-dev.tsc --version and initiate typescript project using the command tsc --init.npx tsc.@types/node helps to add typescript support for Node JS. We can install @types/node using the command npm install @types/node --save-dev.@types/express helps to add typescript support for Express JS. We can install @types/express using the command npm install @types/express --save-dev.