I'd like to use express-boom for express with TypeScript. It lacks of typings so I'd like to write my own. Just make it compile is trivial.
This middleware decorates the res
object with a property boom
(derived from the boom module):
var express = require('express');
var boom = require('express-boom');
var app = express();
app.use(boom());
app.use(function (req, res) {
res.boom.notFound(); // Responsds with a 404 status code
});
But with typescript I need to cast it because neither http.ServerResponse
and Express.Response
have the boom property, of course:
return (<any>res).boom.badRequest('bla bla bla');
Which is the cleanest way to do it? Which are other typed middleware that are doing a similar thing?
There's quite a few other Express middlewares you can use as examples, e.g. Method-Override, and its type definitions.
As a more concrete example, if you want to add this .boom property to the response object, you should just need to create a type definition (express-boom.d.ts) containing:
declare namespace Express {
interface Boom {
// Add boom's properties in here
}
export interface Response {
boom: Boom
}
}
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