Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

444
Views
Express - All declarations of 'user' must have identical modifiers

I am making use of express and passport in my application, In both packages, there's a user attribute under the Request interface. Currently, express has a user attribute but no other properties attached to it in the request object, e.g req.user. I am trying to do a declaration merge (not really sure if this is the right term for it) for express in order to tell typescript that Express.Request.user has an id attribute attached to it like so:

declare namespace Express {
  export interface Request {
    user: {
      id: string
    }
  }
}

At the same time also, passport has a user attribute but with the value of Express.User. When I do that declaration merge above, I get an error that says All declarations of 'user' must have identical modifiers. Here's the declaration file for passport.

interface User {}

        interface Request {
            authInfo?: AuthInfo;
            user?: User;

            // These declarations are merged into express's Request type
            login(user: User, done: (err: any) => void): void;
            login(user: User, options: any, done: (err: any) => void): void;
            logIn(user: User, done: (err: any) => void): void;
            logIn(user: User, options: any, done: (err: any) => void): void;

            logout(): void;
            logOut(): void;

            isAuthenticated(): this is AuthenticatedRequest;
            isUnauthenticated(): this is UnauthenticatedRequest;
        }

I understand it's because of a mismatch in the interfaces, but how do I overcome this? Any help is welcome. Thank you very much!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I had the same issue - I managed to get it working by adding properties directly to the User interface instead of Request.user -

In @types/express/index.d.ts

declare global {
  namespace Express {
    interface User {
      userId: number;
      name: string;
      email: string;
    }
  }
}
over 4 years ago · Santiago Trujillo Report

0

I had the same issue and struggled for an afternoon on it as well. I managed to make it work like so in my project:

declare global {
  namespace Express {
    interface User extends UserDocument {
      // A basic field so EsLint does not think it's an empty interface, 
      // and replace it with some other code which it thinks to be better
      // (cause for me, it was replacing automatically the line with 
      // "type User = UserDocument" which was making Typescript unhappy).
      // This field is useless, but that's the only solution I found.
      uselessField?: boolean;
    }
  }
}

// Alternatively, you can also substitute this line declaration with:
// "export type UserD = mongoose.Document & {"
// as seen in the Microsoft starter template: https://github.com/microsoft/TypeScript-Node-Starter
// It works too.
export interface UserDocument extends mongoose.Document {
  firstName: string;
  lastName: string;
  email: string;
  // ... etc
}

const UserSchema = new mongoose.Schema<UserDocument>(
  {
    firstName: String;
    lastName: String;
    email: String;
    // ... etc
  },
);

export const User = mongoose.model<UserDocument>("User", UserSchema);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!