Your inquiry revolves around dynamically creating an interface from a string union type. To handle scenarios where the fetch function result contains both types of keys, you can iterate over the argument type instead of setting a specific value.
const data: {[key in typeof argument]: string} = await fetchResults('api-url')
It seems unnecessary to further specify the type since the statement data[argument]
will always be of type string based on the provided information.
If there is a need to narrow down the type for specific outcomes, why not directly specify them? You have knowledge of the return structure from the fetch call, so it's feasible to define the exact values.
async function retrieveData<S extends 'type1' | 'type2'>(argument: S){
// fetchResults delivers either {type1:'outcome1'} or {type2:'outcome2'}
const data: {type1: "outcome1", type2: "outcome2"} = await fetchResults('api-url')
return data[argument];
}
(async () => {
const result1 = await retrieveData("type1"); // result1 is of type "outcome1"
const result2 = await retrieveData("type2"); // result2 is of type "outcome2"
})()