Categories
TypeScript Answers

How to explicitly specify types for Express’ application, request, response, etc. with TypeScript?

Spread the love

Sometimes, we want to explicitly specify types for Express’ application, request, response, etc. with TypeScript.

In this article, we’ll look at how to explicitly specify types for Express’ application, request, response, etc. with TypeScript.

How to explicitly specify types for Express’ application, request, response, etc. with TypeScript?

To explicitly specify types for Express’ application, request, response, etc. with TypeScript, we can get the types from the express module.

For instance, we write

import express = require("express");
//...

app.get("/", function (req: express.Request, res: express.Response) {
  //...
});

to require the express module with

import express = require("express");

Then we set the types for req and res to express.Request and express.Response respectively.

Conclusion

To explicitly specify types for Express’ application, request, response, etc. with TypeScript, we can get the types from the express module.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *