-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Description
Acknowledgement
- I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
Description
I’m running into a type augmentation conflict with express
when trying to enforce stricter types for req.user
.
In one of my dependencies, Request
is augmented like this:
declare module "express-serve-static-core" {
interface Request {
user: Record<string, any>;
}
}
In my main project, I want to override this to be stricter:
declare module "express-serve-static-core" {
interface Request {
user: {
id: number;
name: string;
};
}
}
Problem
When I assign req.user = req.session._profile;
inside the dependency, TypeScript complains:
Type '{ [key: string]: any; }' is missing the following properties
from type '{ id: number; name: string; }': id, name
Attempts to merge types using intersections (& Record<string, any>
) or unions don’t fully solve it, because the assignment is done in node_modules
(so I can’t cast or change that code).
I also tried using the new override
modifier in interface augmentations, but TypeScript currently only allows override
on class members, not on interfaces, so ESLint/TS parser rejects it.
What I expect
Ideally, I’d like a way to replace an existing augmentation in a dependency with a stricter one (instead of merging), or at least an idiomatic way to ensure that my project sees req.user
as strict without breaking the assignment in the dependency.
Environment
- TypeScript: 5.9.x
- @typescript-eslint/parser & plugin: v8.x
Question
Is there a recommended/official way to override a property defined in a dependency augmentation (not just merge with it) so that I can enforce stricter types for Request.user
while still allowing the assignment that comes from the dependency?
Or should I instead always redefine the related SessionData
shape (so _profile
matches my stricter user
), and accept that direct overriding of augmentations is not possible?
Thank you in advance