const app = express();
This block is working when I use type any for response object
app.use((_req: Request, response: any, next: NextFunction) => {
response.success = function ({ result = {}, code = 200, message = "" }) {
return response.json({
result, code, message
})
}
next();
})
This block is not working when I use Response type from express because the property success does not exist.
app.use((_req: Request, response: Response, next: NextFunction) => {
response.success = function ({ result = {}, code = 200, message = "" }) {
return response.json({
result, code, message
})
}
next();
})
So I create a new type CustomResponse
interface BaseParams {
code?: number,
message?: string,
result?: any
}
interface CustomResponse extends Response {
success: (params: BaseParams) => Response;
};
app.use((_req: Request, response: CustomResponse, next: NextFunction) => {
response.success = function ({ result = {}, code = 200, message = "" }) {
return response.json({
result, code, message
})
}
next();
})
I get this new error
No overload matches this call.
The last overload gave the following error.
Argument of type '(_req: Request, response: CustomResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'
So I wonder how can I create this kind of global methods using typescript in the right way to avoid this PathParams error type