In TypeScript "Numbers" don't add up
Today I learned that “Numbers” in TypeScript don’t allow the usage of the +
operator to add them to each other. Instead it shows the wonderful compiler error TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'.
. This is explained to be intended behaviour to prevent object overrides to mess up the way those primitive operations work. To fix the problem you simply have to use the native types number
, notice the lower case n
at the beginning.
I came across this while writing a patch at work and looking a bit suspicious at the transpiler output, telling me that it can not add up two numbers. While the sole reason of computer’s existence is pretty much that. With looking a bit deeper into it, it turned out to be semi-reasonable.