Note the brackets round the actual values though. There is an issue in VSCode at least where forcing the type inline doesn't work unless you include the brackets.
Alternatively, you could use Date.parse(d1.getTime() + 1000)) (untested) which should return an integer anyway.
Or Number(new Date()) will also work. Or even (new Date()).getTime() which should also return an integer.
In simple terms, It's something to do with type definitions and what is supported by the subtract operator. Typically objects don't know how to be added/subtracted from one another however some objects (like Date) have a prototype named valueOf that is automatically executed by the V8 engine (under the hood) when you try to run d1+d2
As for whether Monaco should know about this or whether it is a deliberate "good housekeeping" measure - I don't know. I'll look into it some time.
Essentially, you are subtracting numbers so use valueOf...
msg.payload = d2.valueOf() - d1.valueOf()
... Or coerce the type using jsdoc like Julian shows.