TypeScript
TypeScript copied to clipboard
Subtracting Date objects shows a TypeScript error, but actually works.
🔎 Search Terms
"Date arithmetic"
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Dates
⏯ Playground Link
No response
💻 Code
const a = new Date();
const b = new Date();
const c = a - b;
🙁 Actual behavior
TypeScript shows error: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
🙂 Expected behavior
No error because the operation works.
Additional information about the issue
No response
+1
https://stackoverflow.com/questions/41750390/what-does-all-legal-javascript-is-legal-typescript-mean/41750391#41750391
If you want to subtract date objects - please cast them to numbers first.
Duplicate of #5710.
See also https://github.com/microsoft/TypeScript/issues/2361#issuecomment-82509802
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.