是否可以编写将类转换为接口的映射类型减去类的方法或分配给它的任何属性prototype.例如,
class Dog {
name: string;
age: number;
bark() {}
get dogYears(): number {
return this.age * 7;
}
}
type InterfaceOf<T> = { /* ??? */ };
type IDog = InterfaceOf<Dog>;
// equivalent to
interface IDog {
name: string;
age: number;
}
Run Code Online (Sandbox Code Playgroud)
我为什么要这样做? 我希望将json对象"反序列化"为类.例如,我运行一个查询来从数据库中获取狗,之后我想将它们实例化为类对象,也许是通过使用类变换器库.
function queryDogs(): Promise<{ name: string, age: string }[]>;
function deserialize<T>(cls: (new() => T), input: InterfaceOf<T>): T;
function deserialize<T>(cls: (new() => T), inputs: InterfaceOf<T>[]): T[];
class Dog {
@Type(() => String)
name: …Run Code Online (Sandbox Code Playgroud)