adonis-mongodb icon indicating copy to clipboard operation
adonis-mongodb copied to clipboard

Not possible to overload Model methods

Open maasencioh opened this issue 2 years ago • 0 comments

Currently, if I have a model that needs to be validated on the creation I'll try to overload the creation function. The problem is that this leads to the following error

export default class User extends BaseModel {
  @field()
  public _id: ObjectId;
  public firstName: string;
  public lastName: string;
  public fullName?: string;

  public static create(data: Partial<ModelAttributes<User>>) {
    const newData = { ...data, fullName: firstName + lastName };
    return super.create(newData);
  }
}
Class static side 'typeof User' incorrectly extends base class static side '{ readonly $fieldsDefinitions: Map<string, FieldOptions>; $addField(name: string, options?: Partial<FieldOptions> | undefined): FieldOptions; ... 16 more ...; getCollection<ModelType extends MongodbModel<...>>(this: ModelType, connection?: string | undefined): Promise<...>; }'.
  The types returned by 'create(...)' are incompatible between these types.
    Type 'Promise<MongodbDocument<unknown>>' is not assignable to type 'Promise<InstanceType<ModelType>>'.
      Type 'MongodbDocument<unknown>' is not assignable to type 'InstanceType<ModelType>'.

maasencioh avatar Nov 29 '21 13:11 maasencioh