I'm working to update a simple NodeJS + ExpressJS app to use TypeScript. I've got it working, but I'm having fits adding some extra data annotations to add extra type checking and code-time autocomplete/IntelliSense in Visual Studio & WebStorm.
I've got the latest express.d.ts referenced. I've also created the Express app:
var app = express();
Everything works... but I can't seem to figure out how to add the type info for app for autocomplete assistance when you type something like
app.get('/',function(req,res){});
I've tried annotating app (and req & res) using Express.Application
(and .Request
& .Response
) but that isn't resolving. Looking at the type definition I'm getting confused with how the typedef is written creating the internal "e" reference...
Surprised I can't find anyone asking & resolving this since the express.d.ts typedef was updated for ExpressJS 4.0 (previously I found references to ExpressServer but that isn't present today.
Ideas?
The req object represents the HTTP request and has properties for the request query string, parameters, body, and HTTP headers. The res object represents the HTTP response that an Express app sends when it gets an HTTP request.
Introduction. In this article, you will learn about the res object in Express. Short for response , the res object is one half of the request and response cycle to send data from the server to the client-side through HTTP requests.
Request object Express. js is a request & response objects parameters of the callback function and are used for the Express applications. The request object represents the HTTP request and contains properties for the request query string, parameters, body, HTTP headers, etc.
Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.) and URL pattern ("Route"), and methods to specify what template ("view") engine is used, where template files are located, and what template to use to render a response.
Classic case of "work on something for a while with no success, but when you finally post a question asking for help, you figure it out immediately".
The solution: When you want to use one of the types, you have to import the module as the typedef is in a named module within the typedef.
In the above code (that resided in my app.ts), I was getting the type annotations on app. Because my import statement at the top of the file was import express = require('express');
, I could annotate the req & res params like this:
app.get('/', function(req:express.Request, res:express.Response){});
In other files, where I was trying to get type annotations on the app, I was missing the import statement at the top of the file. Once added, I could add the annotations for that as well like:
public init(app: express.Application){}
In case anyone stumbles on this question in 2017 and using a more modern TS. You should be able to just npm install @types/express
and then it should just work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With