I have a container that holds multiple sub-containers, each containing key-value pairs where the value is a URL. I also have a function that takes the name of one of the sub-containers as input, loops through its entries, fetches data from the URLs, and returns an object with the same keys but updated values.
I am seeking a method to dynamically adjust the return type so it aligns with the nested object being processed and returned.
For example:
// URL Object
const URLS = {
Baseball: {
Dodgers: 'url-a1',
Angels: 'url-a2',
Padres: 'url-a3',
},
Football: {
Chargers: 'url-b1',
Rams: 'url-b2',
},
}
// Function for HTTP calls
function getUrlData(sport: string): Record<???, any> {
const values = {}
Object.entries(URLS[sport]).forEach(([team, url]) => {
values[team] = httpGet(url)
})
return values
}
getUrlData('Baseball') // Return type: Record<keyof typeof URLS.Baseball, any>
getUrlData('Football') // Return type: Record<keyof typeof URLS.Football, any>
// Can we achieve something like
Record<keyof typeof URLS[sport], any>