我有一个看起来像这样的基本模型类:
class Base {
abstract public save(): Promise<Base>;
}
我的模型继承自 Base 并且必须实现该save方法。例如,
class User extends Base {
public save(): Promise<User> {
// save my user in the database and return a promise like this...
return getDatabaseConnection()
.then((db) => db.insert(...))
.then((res) => this);
}
}
这工作正常,Typescript 对这里的类型没有问题。
但随着我的发展,我注意到我的模型save方法都非常相似......
class Group extends Base {
public save(): Promise<Group> {
// save my group in the database and return a promise like this...
return getDatabaseConnection()
.then((db) => db.insert(...))
.then((res) => this);
}
}
class OtherModel extends Base {
public save(): Promise<OtherModel> {
// save my other model in the database and return a promise like this...
return getDatabaseConnection()
.then((db) => db.insert(...))
.then((res) => this);
}
}
所以我突然想到,为了保持干燥,我可以实现一个装饰器来将save方法添加到类中。
const save = (table: string, idColumn: string) => {
return function decorator<T extends {new(...args: any[]): {}}>(target: T): T {
return class extends target {
public save = function() {
// save whatever model this is & return it
return getDatabaseConnection()
.then((db) => db.insert(...use the table, idColumn args...))
.then((res) => this);
};
}
};
};
@save('user_table', 'id')
class User extends Base {}
@save('group_table', 'id')
class Group extends Base {}
@save('other_table', 'id')
class OtherModel extends Base {}
它就像一个魅力,除了......打字稿抱怨save我的类声明中缺少抽象方法。
我一直在用@ts-ignore语句解决它,但我想删除它们。
我遇到了这个问题,它是关于通过装饰器添加到类的新方法,我知道装饰器不是为了修改接口或契约,但这不是我在这里做的。我正在尝试实现每个接口存在(并且必须实现)的抽象方法。
当我通过装饰器执行抽象方法时,如何让 Typescript 识别出我确实实现了抽象方法?