Skip to content

Custom Static Methods on a Model Class #689

Description

@lucasm08

Hello, I'm trying to use redux-orm with Typescript, but I ran into an error when I tried use a custom static methods I that I added to a model Class. I'm pretty new to typescript, maybe I'm doing something wrong.

Here are some files

ProductGroup Model:

export class ProductGroup extends Model<typeof ProductGroup, ProductGroupFields> {
    static fields = {
        id: attr(),
        name: attr(),
    };

    static options = {
        idAttribute: "id" as const,
    };

    static parse(productGroupData: GroupFields) {
        return this.upsert(productGroupData);
    }

    toJSON() {
        return { ...this.ref };
    }
}

Usage:

    const session = orm.session(state);

    const {products, contacts} = payload;

    const {ContactGroup,  ProductGroup} = session;


    [ContactGroup, ProductGroup].forEach(modelType => {
        modelType.all().toModelArray().forEach(model => model.delete());
    });

    if(contacts) {
        // tslint:disable-next-line: no-angle-bracket-type-assertion
        contacts.forEach(contact => ContactGroup.parse(contact)); //<------------------- Error. Property 'parse' does not exist on type 'ModelType<ContactGroup>'.
    }

Expectation:
I expect parse to be available on model classes for this Session.

Maybe it's a misuse of typescript, but I would really appreciate if someone can help.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions