【问题标题】:Inherit from parent class extending Typegoose and containing static type-specific methods从扩展 Typegoose 并包含静态类型特定方法的父类继承
【发布时间】:2020-07-02 01:59:18
【问题描述】:

我正在使用 TypeScript 和 Node JS 开发服务器,并且我正在使用 Typegoose 库将类映射到 MongoDB 文档中。

我有以下两个类:

import { prop, getModelForClass, DocumentType, ReturnModelType, Typegoose } from '@typegoose/typegoose';

export default class Mode {
    // ...various attributes
    @prop() name?: string;
    private document?: DocumentType<Mode>;

    public get id(this: Mode): number {
        if (this.document)
            return this.document._id;
        else throw new Error('Looking for id on non-mapped Mode on database');
    }

    private static get model(): ReturnModelType<typeof Mode> {
        return getModelForClass(Mode);
    }

    private static attachDocument(document: DocumentType<Mode> | null): Mode | null {
        const instance: Mode | null = document as Mode | null;
        if (instance && document)
            instance.document = document;
        return instance;
    }
    // other methods...
}
import { prop, getModelForClass, DocumentType, ReturnModelType, Typegoose } from '@typegoose/typegoose';

export default class Player {
    // ...various attributes
    @prop() nickname: string;
    private document?: DocumentType<Player>;

    public get id(this: Player): number {
        if (this.document)
            return this.document._id;
        else throw new Error('Looking for id on non-mapped Player on database');
    }

    private static get model(): ReturnModelType<typeof Player> {
        return getModelForClass(Player);
    }

    private static query(document: DocumentType<Player> | null): Player | null {
        const instance: Player | null = document as Player | null;
        if (instance && document)
            instance.document = document;
        return instance;
    }
    // other methods...
}

很容易注意到有类似定义的方法:id()model()attachDocument()。我需要这些方法来抽象 Typegoose 的行为以及对服务器其余部分的查询执行。有没有办法定义一个超类,使得这三个方法可以从ModePlayer 中删除并由它们从这个超类继承?

我在想这样的事情:

export default class Model<T> extends Typegoose {
    protected document: DocumentType<T>;

    public get id(this: T): number {
        if (this.document)
            return this.document._id;
        else throw new Error('Looking for id on non-mapped object on database');
    }

    private static get model(): ReturnModelType<typeof T> {
        return getModelForClass(T);
    }

    private static attachDocument(document: DocumentType<T> | null): T | null {
        const instance: T | null = document as T | null;
        if (instance && document)
            instance.document = document;
        return instance;
    }
}
export class Player extends Model<Player> { /* ... */ }
export class Mode extends Model<Mode> { /* ... */ }

但是这似乎不可行,因为我不能将 T 作为参数传递给 getModelForClass() 方法。我已经看到它接受new () =&gt; T 类型的参数,但我还没有找到任何正确使用它的方法。

【问题讨论】:

  • getModelForClass(this) 工作吗?
  • 如果你最终解决了这个问题,我很想知道解决方案。

标签: typescript inheritance mongoose typescript-generics typegoose


【解决方案1】:

如果get model() 不需要是private static 方法,你可以试试这个:


class Model<T> extends Object {

    public get model(this: T): T {
        return this;
    }
}

class Player extends Model<Player> { }
class Topic extends Model<Topic> { }

const player = new Player();
const topic = new Topic();

console.log(player.model instanceof Player);
console.log(topic.model instanceof Topic)

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 2011-08-16
    • 2011-06-25
    • 1970-01-01
    • 2011-10-05
    • 2022-01-24
    • 1970-01-01
    • 2022-10-06
    相关资源
    最近更新 更多