首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从父类继承,扩展Typegoose并包含静态类型特定的方法

从父类继承,扩展Typegoose并包含静态类型特定的方法
EN

Stack Overflow用户
提问于 2020-03-21 06:40:54
回答 1查看 1.1K关注 0票数 0

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

我有以下两个类:

代码语言:javascript
复制
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...
}
代码语言:javascript
复制
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中移除,并由它们从这个超类继承?

我在想这样的事情:

代码语言:javascript
复制
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;
    }
}
代码语言:javascript
复制
export class Player extends Model<Player> { /* ... */ }
export class Mode extends Model<Mode> { /* ... */ }

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

EN

回答 1

Stack Overflow用户

发布于 2020-03-21 10:17:44

如果get model()不需要是private static方法,您可以尝试这样做:

代码语言:javascript
复制
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)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60782500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档