Currently, I'm delving into the world of ambient modules and declaration-merging as I attempt to customize the Express TypeScript definition file for Request
.
I have managed to add extra properties, but I am struggling to override the existing ones.
declare global {
namespace Express {
interface Request extends Record<string, unknown> {
addition: "a" | "b";
body: unknown;
cookies: unknown;
route: unknown;
signedCookies: unknown;
}
}
}
// type hinting with "a" | "b"
let addition = express.req.addition
// still any
let overridden = express.req.body
Many questions demonstrate how to augment, but not replace.
I also experimented with the following approach:
declare module "express-serve-static-core" {
namespace Express {
interface Request extends Record<string, unknown> {
addition: "a" | "b";
body: unknown;
}
}
}