I'm currently working on defining an object that has the ability to hold either data or an error.
export type ResultContainer = {
data: any;
} | {
error: any;
};
function exampleFunction():ResultContainer {
return {
data: 3
}
}
However, when attempting to access the result of the function, I encounter the following issue:
const result = exampleFunction();
result.data = 23; // Property 'data' does not exist on type 'ResultContainer'. Property 'data' does not exist on type '{ error: any; }'
Could someone guide me on the correct way to access either 'data' or 'error'? Thank you!