Is there a way in TypeScript to add 40 to a variable of type string | number
, and return a result as a string | number
?
My initial approach was to parse the variable to a number, then perform the addition, but how can I ensure that the variable is properly parsed if it could be either a string or a number?
I attempted the following:
parseInt(x) - 44
which resulted in the following error:
Argument of type 'string | number' is not assignable to parameter of type 'string'.
Type 'number' is not assignable to type 'string'.ts(2345)