投射对象时发生运行时错误

时间:2018-07-26 18:28:57

标签: typescript generics interface casting

我正在一个Typescript项目上工作,尽管代码被很好地编译,并且在概念的所有理论领域中代码都应该起作用,但在运行时却不起作用。

  

我已经缩小了问题的范围,即编译后TypeScript被转换为JavaScript。这意味着没有定义的类和变量类型(通过键入一种解释语言来进行)。

现在,我需要帮助的地方是使其正常工作。下面是类的结构:

基类

从API获得响应时,我将其转换为基类类型

class BaseCollectionTypeResponseModel<TObject>{

    protected responseData: Array<TObject>;

    protected pagination: PaginationResponseModel;

}

PaginationResponseModel定义如下:

class PaginationResponseModel {
    protected totalItems: number;

    protected totalPages: number;

    protected currentPage: number;

    protected itemCount: number;

}

现在,我定义了一个interface函数,ConvertViaAdapter(),如下所示:

interface IConvertable{
    ConvertViaAdapter();
}

实现该接口的示例类如下:

class PlatformResponseModel implements IConvertable{

    protected status: number;

    protected name: string;

    protected platformid: string;

    protected tags: string[];

    protected type: string;

    protected version: string;

    protected description: string;

    ConvertViaAdapter(): PlatformModel {
        return Object.assign(new PlatformModel(), this)
    }
}

现在,我正在创建利用基类功能提供特定于应用程序功能的子类。

儿童班:

例如,分页子类如下所示:     PaginationModel类扩展了APIResponses.PaginationResponseModel {

    get TotalItems(): number{
        return this.totalItems
    }

    get TotalPages(): number{
        return this.totalPages
    }

    get CurrentPage(): number{
        return this.currentPage
    }

    get ItemCount(): number{
        return this.itemCount
    }
}

现在这是棘手的地方:

我已经像下面这样扩展了PlatformResponseModel

class PlatformFunctionalModel extends PlatformResponseModel{

    get Name(): string{
        return this.name
    }

    get IsActive(): boolean{
                if (this.type == 0)
            return True;
                return False;
    }
}

我还使用如下泛型扩展了BaseCollectionTypeModel:

class CollectionTypeBaseModel<TObject extends YObject, YObject extends IConvertable> extends BaseCollectionTypeResponseModel<YObject>{

    private _responseData: Array<TObject> = []
    get ResponseData(): Array<TObject>{
        if (this._responseData == null || this._responseData.length < 1){
            this.responseData.forEach((data)=>{
                this._responseData.push(data.ConvertViaAdapter());
            });
        }
        return this.responseData;
    }
    set ResponseData(data: Array<TObject>){
        this.responseData = data 
        this._responseData = data 
    }

    get Pagination(): PaginationModel{
        return Object.assign(new PaginationModel(), this.pagination)   
    }
}

问题出在上述类的代码行this.responseData.forEach()上,我得到了错误:

  

对象类型没有方法ConvertViaAdapter()

现在,它应该全部正常工作,因为当TObject扩展IConvertable接口时,TObject类型的列表项将具有已定义和实现的功能,否则该代码将无法编译。我需要帮助。

示例JSON

{"responseData":[{"status":2,"name":"HelloSample","platformid":"A1B2C3","tags":["hello","sample"],"type":"delta","version":"v1.0","description":"Just a sample"}],"pagination":{"totalItems":10,"totalPages":5,"currentPage":1,"itemCount":2}}

入门代码:

var objectData = JSON.parse(jsonData);
var myCastedData = Object.assign(new CollectionTypeBaseModel<PlatformFunctionalModel, PlatformResponseModel>(), objectData)

1 个答案:

答案 0 :(得分:0)

我认为示例JSON缺少responseData周围的数组。

由于responseData对象是普通的JavaScript对象,而不是PlatformResponseModel对象,因此您必须想出另一种将转换函数传递到{ {1}}。例如,您可以将其作为构造函数参数传递:

CollectionTypeBaseModel