I created this simple function that returns 2 values - isNumber and convertedValue. I expect isNumber to be boolean type and convertedValue to be number type, but when I extract those values, I get number | boolean type for both values. How can I make changes so when I extract them that I get boolean type for isNumber and number type for convertedValue ?
const numberValidator = (val: string, fn: (str: string) => number) => {
const convertedValue = fn(val);
const isNumber =
typeof convertedValue === 'number' && isFinite(convertedValue);
return [isNumber, convertedValue];
};
const [isNumber, convertedValue] = numberValidator(
val,
Number
); // now type for both values are "number | boolean"