Need Help in typescript for extending the express Response Object
Hello There,
I'm getting this error whenever I'm registering this middleware.
I tried to update the type definition by creating the express.d.ts file in the types folder and updated the tsconfig,json according to that
Code
export interface CustomResponse extends Response {
success: (statusCode: number, data: any, message?: string, records?: string) => Response;
error: (statusCode: number, error: any, message: string) => Response;
}
function customResponseMiddleware(req: Request, res: CustomResponse, next: NextFunction) {
res.success = function (statusCode: number, data: any, message = 'Success', records?: string) {
return res.status(statusCode).json({
status: 'success',
message,
data,
records
});
}; // same for error response :- res.error()
next();
}
Code
export interface CustomResponse extends Response {
success: (statusCode: number, data: any, message?: string, records?: string) => Response;
error: (statusCode: number, error: any, message: string) => Response;
}
function customResponseMiddleware(req: Request, res: CustomResponse, next: NextFunction) {
res.success = function (statusCode: number, data: any, message = 'Success', records?: string) {
return res.status(statusCode).json({
status: 'success',
message,
data,
records
});
}; // same for error response :- res.error()
next();
}
// error message
No overload matches this call.
The last overload gave the following error.
Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: CustomResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.
// error message
No overload matches this call.
The last overload gave the following error.
Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: CustomResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"baseUrl": "./src",
"paths": {
"*": [
"types/*"
]
}
},
"include": [
"src/**/*.ts",
"types/**/*.d.ts"
]
}
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"baseUrl": "./src",
"paths": {
"*": [
"types/*"
]
}
},
"include": [
"src/**/*.ts",
"types/**/*.d.ts"
]
}
// express.d.ts
I tried solution from the stackoverflow still got the same error
declare namespace Express {
export interface Response {
success: (statusCode: number, data: any, message?: string, records?: string) => this;
error: (statusCode: number, error: any, message: string) => this;
}
}
// express.d.ts
I tried solution from the stackoverflow still got the same error
declare namespace Express {
export interface Response {
success: (statusCode: number, data: any, message?: string, records?: string) => this;
error: (statusCode: number, error: any, message: string) => this;
}
}
0 Replies