Typescript refactoring

Viewed 45

This code works correct . But it looks weird for me . Especially this part ``` const city = [].concat(...locationCities).includes('London'); if(city === true)

apiClient:

export interface apiRequest {
  locations: LocationOptions[];

}
interface FOLocations {
  locationId: number;
}

interface LocationResult {
  location: FOLocations[];
  address: Address;
}
interface LocationResult {
country:string;
city:string;
}
export const locationCheck = async (
  apiKey: string,
  payload: apiRequest
): Promise<LocationResult[]> => {
  let response = await axios.post<LocationResult[]>(
    `api...`,
    payload
  );
  return response.data;

runCode:

  const locationPayload : apiRequest = {
      locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
    };
    const locationResponse = await locationCheck(apiKey,locationPayload);
    const locationCities = locationResponse.map((location) => location.address.map(options => {return options.country}));
    console.log(locationCities);
    const city = [].concat(...locationCities).includes('London');
    console.log(city);
  if(city === true){
      console.log(`London location exist`);
    }else {
      throw new Error(
            "error"
      );
    }

Output :

true
London location exist
1 Answers

I think you can delete this line:

const city = [].concat(...locationCities).includes('London');

And replace this:

if(city === true){
  // ...

With this:

if(locationCities.includes('London')){
  // ...
Related