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

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.