【问题标题】:Express - All declarations of 'user' must have identical modifiersExpress - 'user' 的所有声明必须具有相同的修饰符
【发布时间】:2021-05-29 19:35:59
【问题描述】:

我在我的应用程序中使用了expresspassport,在这两个包中,Request 接口下都有一个user 属性。目前,express 有一个 user 属性,但在请求对象中没有附加其他属性,例如 req.user。我正在尝试为 express 进行声明合并(不确定这是否是正确的术语),以便告诉 typescript Express.Request.user 附加了一个 id 属性,如下所示:

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

同时,passport 也有一个user 属性,但其值为Express.User。当我在上面进行该声明合并时,我收到一条错误消息,上面写着All declarations of 'user' must have identical modifiers. 这是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;
        }

我知道这是因为接口不匹配,但我该如何克服呢?欢迎任何帮助。非常感谢!

【问题讨论】:

  • 您找到解决方案了吗?有同样的问题。

标签: javascript node.js typescript express


【解决方案1】:

我遇到了同样的问题 - 我设法通过将属性直接添加到 User 接口而不是 Request.user 来使其工作 -

@types/express/index.d.ts

declare global {
  namespace Express {
    interface User {
      userId: number;
      name: string;
      email: string;
    }
  }
}

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,也为此苦苦挣扎了一个下午。我设法让它在我的项目中像这样工作:

    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);
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 2018-11-11
      • 2017-08-13
      • 2023-03-21
      • 2017-11-15
      • 2018-04-18
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多