I´m encountering a
TypeError: type is not a constructor
when attempting to convert API data into Frontend DTOs.
The transformation method is as follows:
transform<T>(entities: any[]) {
const tableDtos = new Array<T>();
for (const entity of entities) {
const dto: T = this.factory.createEntity(entity);
tableDtos.push(dto);
}
return tableDtos;
}
My factory looks like this:
export class Factory {
create<T>(type: (new () => T)): T {
return new type();
}
}
I found the solution here. What could I be missing?
DTO
import { Entity} from 'src/models/api/Entity';
export class EntityTableEntry {
id: number;
name: string;
constructor(entity: Entity) {
this.id = entity.ID;
this.name = entity.Name;
}
}
Entity
export interface Cost {
ID: number;
Name: string;
Description: string;
Value: number;
}
The reason behind wanting a generic method is to avoid rewriting the transform method for each API call, which can become messy!