zakodium / adonis-mongodb

MongoDB provider for AdonisJS 5
https://zakodium.github.io/adonis-mongodb/
Other
64 stars 12 forks source link

Not possible to overload Model methods #124

Open maasencioh opened 2 years ago

maasencioh commented 2 years ago

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>'.