Currently, I am facing a challenge with a function that takes an argument and returns a different type of value depending on the argument's value.
For instance:
function foo(arg: 'a' | 'b') {
if (arg === 'a') {
return { prop1: 'hello', prop2: 123 };
} else {
return [1, 2, 3];
}
}
I am struggling to properly type check this function. My main objective is to ensure that when the argument is 'a', the returned value contains properties like prop1 and prop2. On the other hand, if the argument is 'b', the returned value should be an array of numbers.
Although I attempted to utilize conditional types for this purpose, I have encountered some difficulties in making it work effectively. Could anyone offer assistance or guidance on how to achieve this?