I have a function that returns multiple types (union type) but then I have another function later that uses the value returned from the first function but only accepts a single type. Right now typescript is giving me an error saying that there is a type mismatch because the first function could potentially return something that would not be acceptable for the second function, but in the way that I am calling it I know it will not have a conflict. How can I inform typescript that the return value of the first method will work in the second?
Example:
function returnNumberOrString (variable): string | number {
if (variable === 'hello') return 'hello to you';
if (variable === 123) return 456;
}
function onlyNumbers (number : number) {
return number + 1;
}
let number = returnNumberOrString(123);
onlyNumbers(number); // This shows a warning because returnNumberOrString could possibly return a string