So, I have this TypeScript function that will return one of two different objects based on the input value:
function myfunc(isError:boolean): {response:string}|{error:string} {
if(isError) return {error:''}
return {response:''}
}
Now, when I attempt to use this function and access a property from the returned object, I encounter a TypeScript error:
const answer = myfunc(true);
if(answer.error) {...}
else return answer.response;
The TypeScript error says: Property 'error' does not exist on type '{response:string}|{error:string}'
I am aware that I can check for each individual property in the object, but ideally I would like to avoid doing so repetitively.