代码之家  ›  专栏  ›  技术社区  ›  Mladen

猫鼬型干扰

  •  0
  • Mladen  · 技术社区  · 7 年前

    isDeleted() isDeleted 领域

    我有这样的想法:

    export class Factory<E> {
      private readonly _model: Model<Document & E>;
    
      public get model(): Model<Document & E> {
        return this._model;
      }
    
      constructor(config: IFactoryConfiguration) {
        // ...
        let schema: Schema = new Schema(this.definition);
        this._model = this.connection.model<Document & E>(this.name, schema);
      }
    }
    

    那么我有:

    export class UserFactory extends Factory<IUser> {
      constructor(connection: Connection) {
        super({
          connection: connection,
          name: 'User',
          definition: UserSchema
        });
      }
    }
    

    以及:

    export const UserSchema: SchemaDefinition = {
      // ...
      isDeleted: {
        type: Boolean,
        default: false
      } // ...
    }
    

    IUser isDeleted: boolean; 财产等。

    现在,我想在每次启动服务器时创建/更新系统用户:

    let system = await this.factories.user.model.findOne({
      'isSystem': true
    });
    
    if (!system) {
      system = new this.factories.user.model();
      system.isSystem = true;
      system.isDeleted = true; <-- error here
      await system.save();
    }
    

    问题是我有 属性(在本例中为 尤瑟 ),但猫鼬有 isDeleted() 方法在它的 Document Intersection Type 属于 Document & IUser ,这里有干扰。我得到的错误是:

    错误:(239,7)TS2322:类型“true”不能分配给类型“({(isDeleted:boolean):void;():boolean;}&false);({(isDeleted:boolean):void;():boolean;}&true)”。

    因为有 isDeleted(): boolean; 方法(看一看 here ).我如何解决这个问题?

    "mongoose": "^4.13.17",
    "@types/mongoose": "^4.7.23",
    

    "mongoose": "^5.4.7",
    "@types/mongoose": "^5.3.10",
    

    我正在使用

    "typescript": "^3.2.2",
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   dHour    7 年前

    也许您可以在设置 isDeleted

    比如:

    <IUser>system.isDeleted = true