I have a function that detects whether a type could be a number and changes it to Float whenever posible, this is quite usefull to me when getting data converted from csv to JSON that stringifies everything.
const possibleNum: string | number = '3'
export const changeType = (entry: string | number) => {
return !isNaN(parseFloat(entry)) ? parseFloat(entry) : entry
}
const res = changeType(possibleNum)
console.log(typeof res)
// number
This works well with regular JavaScript, but TypeScript is not having it.
I get
`Argument of type 'string | number' is not assignable to parameter of type 'string'.
Type 'number' is not assignable to type 'string'.ts(2345)`
How can I do it?