Javascript 可以在运行时迭代Typescript类抽象方法吗?
我需要知道这个类的抽象方法(实际上是所有方法,包括抽象方法)的列表。是否可以使用Typescript进行某种操作Javascript 可以在运行时迭代Typescript类抽象方法吗?,javascript,typescript,oop,Javascript,Typescript,Oop,我需要知道这个类的抽象方法(实际上是所有方法,包括抽象方法)的列表。是否可以使用Typescript进行某种操作 export abstract class INotificationService { abstract dismissRequested(); } console.log(Object.getMethodsList(INotificationService)); 预期结果:['dismissRequested',…]没有为抽象方法生成代码,因此没有直接获取方法的方法
export abstract class INotificationService {
abstract dismissRequested();
}
console.log(Object.getMethodsList(INotificationService));
预期结果:['dismissRequested',…]没有为抽象方法生成代码,因此没有直接获取方法的方法。您可以创建一个虚拟实现并从中获取函数:
abstract class INotificationService {
abstract dismissRequested(): void;
}
function getMethods<T>(cls: new (...args: any[]) => T): string[] {
return Object.getOwnPropertyNames(cls.prototype).filter(c=> c!=="constructor");
}
var methods = getMethods<INotificationService>(class extends INotificationService {
dismissRequested(): void {
throw new Error("Method not implemented.");
}
});
抽象类INotificationService{
抽象解雇请求():无效;
}
函数getMethods(cls:new(…args:any[])=>T:string[]{
返回Object.getOwnPropertyNames(cls.prototype).filter(c=>c!=“构造函数”);
}
var methods=getMethods(类扩展了INotificationService{
dismissRequested():void{
抛出新错误(“方法未实现”);
}
});
如果我们愿意,我们可以通过禁止虚拟实现类使用任何新方法使其更安全。这将防止我们忘记从抽象类中删除的旧方法,尽管虚拟实现可以覆盖非抽象的现有类方法,因此请谨慎使用:
type Diff<T extends string, U extends string> = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T];
function getMethods<T>(): <TResult>(cls: new (...args: any[]) => TResult & { [ P in Diff<keyof TResult, keyof T>]: never }) => string[] {
return cls => Object.getOwnPropertyNames(cls.prototype).filter(c=> c!=="constructor");
}
abstract class INotificationService {
abstract dismissRequested(): void;
nonAbstarct(): void {}
}
var methods = getMethods<INotificationService>()(class extends INotificationService {
// Implement abstract methods, although it is possible to add other methods as well and the compiler will not complain
dismissRequested(): void {
throw new Error("Method not implemented.");
}
});
// Will cause an error
var methods2 = getMethods<INotificationService>()(class extends INotificationService {
dismissRequested(): void {
throw new Error("Method not implemented.");
}
oldDismissRequested(): void {
throw new Error("Method not implemented.");
}
});
// Will NOT cause an error
var methods3 = getMethods<INotificationService>()(class extends INotificationService {
dismissRequested(): void {
throw new Error("Method not implemented.");
}
nonAbstarct(): void {
throw new Error("Method not implemented.");
}
});
type Diff=({[P in T]:P}&{[P in U]:never}&{[x:string]:never}[T];
函数getMethods():(cls:new(…args:any[])=>TResult&{[P in Diff]:never}=>string[]{
返回cls=>Object.getOwnPropertyNames(cls.prototype.filter)(c=>c!=“构造函数”);
}
抽象类INotificationService{
抽象解雇请求():无效;
nonAbstarct():void{}
}
var methods=getMethods()(类扩展了INotificationService{
//实现抽象方法,尽管也可以添加其他方法,而且编译器不会抱怨
dismissRequested():void{
抛出新错误(“方法未实现”);
}
});
//将导致错误
var methods2=getMethods()(类扩展了INotificationService{
dismissRequested():void{
抛出新错误(“方法未实现”);
}
oldDismissRequested():void{
抛出新错误(“方法未实现”);
}
});
//不会导致错误
var methods3=getMethods()(类扩展了INotificationService{
dismissRequested():void{
抛出新错误(“方法未实现”);
}
nonAbstarct():void{
抛出新错误(“方法未实现”);
}
});