Module Augmentation
Prerequisites: Declaration files
Module augmentation adds new types to an existing module you've imported — common when a library expects you to extend one of its own interfaces.
// express.d.ts
import "express";
declare module "express" {
interface Request {
userId?: string;
}
}
This adds a userId property to Express's own Request type
project-wide, so req.userId type-checks anywhere Request is used
— a common pattern for adding authentication middleware's data to a
framework's built-in types.