2

In the code below I get the following error: Property 'latlng' of type 'LatLng[]' is not assignable to string index type 'string[]'.ts(2411). Am I not allowed to have a string index in Typescript? How can I fix this?

interface LatLng {
  lat: number;
  long: number;
}
export interface Country {
  [countryName: string]: Capital[];
  latlng: LatLng[];
}

type Capital = string;

This is how I am setting the array from the API

 const randomCountries = randomCountriesFull.map((country) => {
        return {
          [country.name.common]: country.capital,
          latlng: country.latlng,
        };
      });
  • 1
    would be helpful if you can add some context as to what you're trying to achieve, its not so clear from this – Max Apr 14 '23 at 16:43
  • 1
    because rn there's a conflict because you're creating Country as string -> Capital[] and also saying latlng -> LatLng[] . You can see if you change from string to string&{latlng:never} it will accept but may be cleaner approaches – Max Apr 14 '23 at 16:47
  • @max I added some code. – Michael Horvilleur Apr 14 '23 at 16:52
  • 1
    What does your `country.latlng` look like? It should be something like this to work correctly `[{ lat: 12.345, lng: 12: 12345 }, ...]` – Mushroomator Apr 14 '23 at 16:57
  • 2
    This is not directly possible; see the answer to the linked question for the various options available. – jcalz Apr 14 '23 at 17:14

0 Answers0