Encountering a familiar issue with what appears to be a simple fix. The Express Request object includes a user property that is specified as Express.User (an empty object).
Attempting the common approach to redefining it:
// index.d.ts
import { User as PrismaUser, Profile } from "@prisma/client";
declare global {
namespace Express {
export interface Request {
user: PrismaUser & { profile: Profile };
}
}
}
This file is included in my tsconfig.json
settings.
However, when trying the above solution, I encounter the following error message:
All declarations of 'user' must have identical modifiers.ts(2687)
Subsequent property declarations must have the same type. Property 'user' should be of type 'User', but currently has type 'User & { profile: Profile; }'.
Apparently, it is required to remain typed as Express.User.
In contrast, the following modification works:
declare global {
namespace Express {
export interface Request {
currentUser: PrismaUser & { profile: Profile };
}
}
}
This allows me to use request.currentUser
in my code.
Why am I unable to change the type of the user property despite seeing numerous solutions suggesting otherwise? It seems like my error is unique. Could it be an issue with my tsconfig setup?