I want to use Express.js with Typescript to specify the code more modular/OO.
I want to implement a Route/Controller by implementing the IRoute interface and then adding the new Route to app.use().
The problem I'm facing is that each operation (get,post,put,delete) itself returns the IRoute interface and I'm not sure what to return to. To return
return <IRoute> this; in an operation is not working.
The typescript compiler response with the following error message:
Class MessurmentsController incorrectly implements interface IRoute. Types of property
allare incomatible. Type(req: Request, res: Response, next: Function) => voidis not assignable totype (...handler: RequestHandler[]): IRoute. Types of parametersreqandhandlerare incomatible. TypeRequestis not assignable to typeRequestHandler.
/// <reference path="../../../typings/tsd.d.ts" />
import {IRoute, Request,Response} from 'express';
export class MeasurementsController implements IRoute {
path: string;
stack: any;
constructor(){
this.path = "/api/measurements"
}
all(req: Request, res: Response, next: Function){
res.send('');
//return <IRoute> this;
}
get(req: Request, res: Response, next: Function){
res.send('');
}
post(req: Request, res: Response, next: Function){
res.send('');
}
put(req: Request, res: Response, next: Function){
res.send('');
}
delete(req: Request, res: Response, next: Function){
res.send('');
}
patch(req: Request, res: Response, next: Function){
res.send('');
}
options(req: Request, res: Response, next: Function){
res.send('');
}
head(req: Request, res: Response, next: Function){
res.send('');
}
}
The Route in d.ts is defined as
module e {
interface IRoute {
path: string;
stack: any;
all(...handler: RequestHandler[]): IRoute;
get(...handler: RequestHandler[]): IRoute;
post(...handler: RequestHandler[]): IRoute;
put(...handler: RequestHandler[]): IRoute;
delete(...handler: RequestHandler[]): IRoute;
patch(...handler: RequestHandler[]): IRoute;
options(...handler: RequestHandler[]): IRoute;
head(...handler: RequestHandler[]): IRoute;
}
Any Idea of what I need to return in an operation to get this working?