Skip to content

Basic model type #1766

@kristijan11b

Description

@kristijan11b

Describe the feature

Hi,

I wanted to implement a type-safe method to insert data into the repository. But after checking the type definitions of pinia-orm, I haven't found a type that will represent the basic state of the model. I was expecting to find something like: Record<keyof Model, any>

Lets take a look at this example:

export class Song extends Model {
  static entity = 'song';

  @Uid()
  declare id: string;
}
add(song: Song) {
  this.repo.insert(song);
}

If I try to insert data into repo, I will get the following error:
Argument of type '{}' is not assignable to parameter of type 'Song'.
Type '{}' is missing the following properties from type 'Song': id, _meta, $self, $entity, and 39 more.

Yes, I can use Element istead of Song as type of parameter, but this type is not type-safe, it doesn't represent actual model field.

Can you create a type that will allow me to insert data type-safe?
Bad:
.add({ id: 1, foo: 'bar' }) //BAD
.add({ id: 1, }) //GOOD

Additional information

  • Would you be willing to help implement this feature?

Final checks

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions