Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS2362 - Error when doing arithmetic operations on date #5710

Closed
erichillah opened this issue Nov 18, 2015 · 3 comments
Closed

TS2362 - Error when doing arithmetic operations on date #5710

erichillah opened this issue Nov 18, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@erichillah
Copy link

Hi,
The following valid js code compile with TS2362 error

let start = new Date();
let elapsed = new Date() - start;
index.ts(9,22): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
index.ts(9,37): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

To avoid the error i need to explicitely cast opreands wit <any>.

@weswigham
Copy link
Member

Duplicates #2361 since this works because valueOf a Date is a number.

@weswigham weswigham added the Duplicate An existing issue was already created label Nov 18, 2015
@weswigham
Copy link
Member

BTW, a canonical workaround (and generally safer thing to do) is this:

let start = +new Date();
let elapsed = +new Date() - start;

Unary + will coerce to number.

@erichillah
Copy link
Author

Thanks, I've checked #2361 and didn't know all that about the Date object. Happy enough with the unary + operator.
Closing this issue.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants