Specifying a type
in TypeScript does not handle the conversion. You have to do that yourself.
In your example, the argument being passed to your getDir
function is a string and not a number.
The exact code you have posted in your answer does what you want it to (produces 20
). You can check that out here
If you don't handle the conversion, string + string
in javascript will be a concatenation and not an addition.
There are multiple ways to convert strings to number in JavaScript. The most simple way is to throw a +
in front of your number. (+'10' + 10
)
Example:
console.log('Should be 20: ', 10 + 10)
console.log('Should be 1010: ', '10' + 10)
console.log('Should be 20: ', +'10' + 10)