1

Since we can not overload function in JS, i try to pass different types, which 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)

CourseRaw and CourseNameSearchRaw are both interfaces with different object structures:

export interface CourseNameSearchRaw {
  trackId: number;
  typeCode: number;
  trackName: string;
}

My idea was to handle each type differently and return the respect type.

1 Answers1

0

In similar cases I always prefer going with Named params, when I am defining a function. This way wherever you have to call the function you have to specify a Key as well, instead of providing a single value.

For example:

const func = ({ key1, key2, key3 = “defaultValue3” }) => {
    if (key1) {}
    if(key2) {} 
    ....etc
}

And of course you can define an interface with types for each key. Hope this solution works for you! :)

Todor Popov
  • 181
  • 7