TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Subtracting Date objects shows a TypeScript error, but actually works.

Open ScottHenshaw86 opened this issue 9 months ago • 4 comments

🔎 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

ScottHenshaw86 avatar May 02 '24 07:05 ScottHenshaw86

+1

pavanputhra avatar May 02 '24 07:05 pavanputhra

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.

Andarist avatar May 02 '24 07:05 Andarist

Duplicate of #5710.

MartinJohns avatar May 02 '24 08:05 MartinJohns

See also https://github.com/microsoft/TypeScript/issues/2361#issuecomment-82509802

RyanCavanaugh avatar May 02 '24 15:05 RyanCavanaugh

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar May 05 '24 01:05 typescript-bot