【发布时间】:2020-06-03 07:43:59
【问题描述】:
假设你有这两个类:
class Form {
static fields(){
return []; // some magic happens here
}
}
class UserForm extends Form{
email: string;
}
const formFields = UserForm.fields();
我可以为静态fields 方法添加什么类型以获取UserForm 属性列表,在这种情况下,只有电子邮件属性为("email")[]?
我尝试使用keyof T,但它没有给我想要的答案:
static fields<T>(this: T): (keyof T)[] {
return []; // some magic happens here
}
这将返回 ("prototype" | "fields")[] 而不是 ("email")[]。
您对如何解决这个问题有任何想法吗?
谢谢!
【问题讨论】:
标签: typescript class oop types