我有一个抽象类
export abstract class ABaseModel {
public static isKeyOf<T>(propName: (keyof T)): string {
return propName;
}
}
另一个扩展它的课程
export class Model extends ABaseModel {
public id: number;
public uid: string;
public createdByUid: string;
}
要检查字符串是否是该类的有效属性,我必须
Model.isKeyOf<Model>('id');
但我想写
Model.isKeyOf('id');
类型推断不可能吗?
答案 0 :(得分:3)
这似乎有效:
abstract class ABaseModel {
public static isKeyOf<T>(this: { new(): T }, propName: (keyof T)): string {
return propName;
}
}
Model.isKeyOf("id"); // ok
Model.isKeyOf("name"); // error: Argument of type '"name"' is not assignable to parameter of type '"id" | "uid" | "createdByUid"'