I am struggling with finding the right data type for my function, where I need to work with static types.
I have experimented with Type
, interface
, class
, Record
, and others, but none seem to fit perfectly.
GEOLOCATIONS
is a constant record that maps country names to their abbreviated codes. The challenge arises when defining the type for the location
argument in my async function changeGeoLocation
.
const GEOLOCATIONS: Record<string, string> ={
Canada: "CA",
USA: "US",
Denmark: "Da"
}
export const changeGeoLocation = async (location: keyof GEOLOCATIONS) => {
const url = `${baseUrl}/en`;
await puppeteer.launch({ headless: false }).then(async (browser) => {
let page = await browser.newPage();
page.goto(url).then(async () => {
const cookieValue = GEOLOCATIONS[location]
await page.setCookie({ name: "ucISO", value: });
await page.reload();
});
});
};
The main issue lies in restricting the location
argument to only accept keys from the GEOLOCATIONS
object and accessing the corresponding values within the function. What would be the most suitable type approach in this scenario?