TypeScript: Cannot perform arithmetic on Dates
new Date() - new Date()
// [ts] The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
JS will implicitly convert these to numbers via valueOf, so explicit conversion is not necessary.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 19 (11 by maintainers)
@bcherny nearly everything TypeScript flags as an error has some valid runtime interpretation. If you want to use
[ ] * { }as a way to produceNaN, well, that’s according to spec, so TypeScript shouldn’t disallow it?This is really a dupe of #2361 because
DateimplementsvalueOfthat sometimes returnsnumber.It’s not being ignored, it’s omitted by design. Implicit conversions are the source of bugs. If all valid ES was valid TS there would not be much point to TS.
If one really wants to subtract dates then that issue shows how to convince the compiler to allow it.