// Adding a typed field to a request through your middleware chain// authed.d.tsimport'express';
declaremodule'express-serve-static-core' { // express's real type homeinterfaceRequest {
user?: { id: string; roles: string[] };
}
}
export {}; // ensure this file is a module so `declare module` augments
Пояснение
export {} превращает файл в module — без этого declare module может не сработать как augmentation. Для Express типы часто живут в express-serve-static-core, не в 'express'.
Итог
Hand-written .d.ts — второй источник правды; держите shape близко к runtime и предпочитайте declaration: true из исходников.
TypeScript: файлы деклараций (`.d.ts`) — пример кода · Sobeso