I am currently tackling a project in Typescript
. Even though the code compiles without errors and adheres to all theoretical principles, it fails to function correctly at Runtime.
The root cause of this issue lies in the fact that
TypeScript
is transpiled intoJavaScript
post compilation. This means there are no explicit classes or variable types (it follows dynamic typing, similar to interpreted languages).
Now, I am seeking assistance to resolve this matter. Below is an outline of the class structure:
The Base Classes
Upon receiving an API response, I cast it to a base class type
class BaseCollectionTypeResponseModel<TObject>{
protected responseData: Array<TObject>;
protected pagination: PaginationResponseModel;
}
The structure of PaginationResponseModel is outlined below:
class PaginationResponseModel {
protected totalItems: number;
protected totalPages: number;
protected currentPage: number;
protected itemCount: number;
}
Additionally, I have defined an interface
containing a method ConvertViaAdapter()
, as shown below:
interface IConvertable{
ConvertViaAdapter();
}
A sample class implementing the interface is detailed here:
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)
}
}
In addition to these base classes, I'm creating child classes to leverage base class functionalities for application-specific features.
The Child Classes:
For example, the pagination child class resembles the following: class PaginationModel extends 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
}
}
Challenges arise when extending the PlatformResponseModel
with further functionality:
class PlatformFunctionalModel extends PlatformResponseModel{
get Name(): string{
return this.name
}
get IsActive(): boolean{
if (this.type == 0)
return True;
return False;
}
}
Similarly, the BaseCollectionTypeModel has been extended using Generics:
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)
}
}
The issue arises in the line this.responseData.forEach()
within the above class, resulting in the error:
Object type doesn't have the method
ConvertViaAdapter()
.
According to expectations, instances of TObject should implement the ConvertViaAdapter()
function when extending the IConvertable
interface. However, this seems not to be the case. Assistance is requested.
Sample 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}}
Starter code:
var objectData = JSON.parse(jsonData);
var myCastedData = Object.assign(new CollectionTypeBaseModel<PlatformFunctionalModel, PlatformResponseModel>(), objectData)