I am facing compile errors while attempting to convert an Object containing an Array of Objects.
Here is the initial structure:
export interface CourseRaw {
metaInfo: MetaInfoRaw;
gameCode: number;
gameText: string;
images?: ImageRaw[]; // Having issues here
}
export interface ImageRaw {
uploaded: string;
url?: string;
description?: string;
fileName?: string;
}
The desired outcome should be typed objects (interfaces) with some properties omitted for clarity, notably changing the type of "Image" from "ImageRaw". Note that "Image" now includes "uploaded" as a Date instead of a string as in "ImageRaw"
export interface Image {
uploaded: Date;
url?: string;
description?: string;
fileName?: string;
}
In my transformation function, I attempted to duplicate the data into a new array, but encountered compiler errors:
export class CourseFactory {
static fromRaw(courseRaw: CourseRaw): Course {
return {
...courseRaw,
ratingInfo: RatingFactory.fromRaw(courseRaw.ratingInfo), // Nested object example (successful)
images: ImageFactory.fromRaw(courseRaw.images) // Issue arises here
};
}
My failed attempt at copying the data:
static fromRaw(imageRaw: ImageRaw[]): Image[] {
var newImages;
var i;
for (i = 0; i < imageRaw.length; i++) {
newImages[i].uploaded = new Date(imageRaw[i].uploaded);
newImages[i].url = imageRaw[i].url;
newImages[i].description = imageRaw[i].description;
newImages[i].fileName = imageRaw[i].fileName;
}
return newImages;
}
How can I resolve this issue?