Angular 4 typescript parsing enum interface attribute in response object

Viewed 7600

I have a response from API which is returning a enum value. value returned from API is represented as a string in request. This value is a enum attribute of typescript interface.

Problem: when receiving a response, TS interface store that value as a string (probably thats the issue) so i can't use it directly as enum.

obj model:

export interface Condo {

  id:number
  title:string
  latitude:number
  longitude:number

  city:string
  country:string
  district:string
  address:string
  locationType: LocationType
}

export enum LocationType {
  CONDO,
  MALL,
  STATION
}

request:

getCondoAllByCountry(country_code){
    return this.http.get(this.config.apiEndpoint +this.subApiUrl+'/all')
      .map(res => <Condo[]>res.json())
      .catch((err:Response) => {
        return Observable.throw(err.json());
      });
    }

usage sample:

    this.condoService.getCondoAllByCountry(this.userData.country_code).subscribe(data=>{
          someFunc(data)
        })

............
    someFunc(condo_list: Condo[]){
    //here is need to know the `locationType` for each object
      console.log(typeof condo_list[i].locationType);
      console.log(typeof LocationType.CONDO)
      switch (condo_list[i].locationType){
        case LocationType.CONDO:
          console.log('Case - condo')
          break;
        case LocationType.MALL:
          console.log('Case - mall')
          break;
        case LocationType.STATION:
          console.log('Case - station')
          break;
      }
    }

So, the switch.. caseis not working for this attribute. in console.log() i'm getting:

console.log(typeof condo_list[i].locationType); - string

console.log(typeof LocationType.CONDO) - number

So, that means that there were a parsing prob and condo_list[i].locationType is not a enum (considering it should show number for enum)?

How should i fix it?

1 Answers
Related