网站搜索

typescript - Angular 4 Http JSON 反序列化和泛型


我有一个 API,会返回如下的 JSON 对象:

{
    "name": "testing",
    "theme": "something"
    "themeProperties": {}
}

themeProperties 的对象类型会根据 theme 的值而变化。我正在尝试这样定义这些类型:

export class Config<T> {
    public name: string;
    public theme: string;
    public themeProperties: T
}


export class Style1Props extends Config<Style1Props> {


    public fooProp1: number;
    public fooProp2: boolean
}

export class Style2Props implements Config<Style2Props> {

    public barProp1: any;
    public barProp2: string;
    public createdDate: Date;
}

public class ConfiService {

    public getConfig<T>() : T {

    }
}

我会在我的组件中这样使用它:

let config = this.configService.getConfig<StyleProps2>()

我的问题是我的数据全部来自一个 API,并且这些类不是简单的原始数据类型。它们内部包含数组和类(尤其是 Date 类),而由于默认的反序列化这些方法不可用。我研究了很多 TypeScript 的反序列化工具,但没有一个能处理泛型。我该如何深度反序列化我的类及其子类?