Harsh Rao
Harsh Rao
KPCKevin Powell - Community
Created by Harsh Rao on 8/6/2024 in #back-end
Need Help in typescript for extending the express Response Object
Hello There,
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();
}
I'm getting this error whenever I'm registering this middleware.
// 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'.
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
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;
}
}
1 replies