With the limitation of not being able to overload functions in JS, my attempt to pass different types has failed:
static fromRaw(raw: CourseRaw | CourseNameSearchRaw): Course | CourseNameSearch {
if (raw instanceof CourseRaw) {
return {
...raw,
metaInfo: MetaInfoFactory.fromRaw(raw.metaInfo),
// terrain: TagFactory.fromRaw(courseRaw.terrain),
ratingInfo: RatingFactory.fromRaw(raw.ratingInfo),
images: ImageFactory.fromRaw(raw.images)
};
}
}
--> 'CourseRaw' only refers to a type, but is being used as a value here.ts(2693)
Both CourseRaw and CourseNameSearchRaw are interfaces with distinct object structures:
export interface CourseNameSearchRaw {
trackId: number;
typeCode: number;
trackName: string;
}
My approach was to handle each type differently and return the respective type.