You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on.
Currently, the lib.d.ts definition states that this function returns number.
We could change it to return 0 | 1 | ... | 11 to encode that return semantic.
This would be beneficial if user code contains something like if (date.getMonth() == 12). The compiler can then infer that the entire if branch is dead code.
Also, it serves as a quick documentation to let the user know that the return value is in fact a 0-based (not 1-based) month.
I'm not sure if the narrowing of the return type would cause a breaking change.
If this is up for grabs, I'd take this.
The text was updated successfully, but these errors were encountered:
nikeee
changed the title
Date.getMonth could be a union of all possible values
Date.getMonth could return a union of all possible values
Jan 21, 2020
According to the MDN docs,
Date.getMonth()
always returns a number between0
and11
(inclusive):Currently, the
lib.d.ts
definition states that this function returnsnumber
.We could change it to return
0 | 1 | ... | 11
to encode that return semantic.This would be beneficial if user code contains something like
if (date.getMonth() == 12)
. The compiler can then infer that the entire if branch is dead code.Also, it serves as a quick documentation to let the user know that the return value is in fact a 0-based (not 1-based) month.
I'm not sure if the narrowing of the return type would cause a breaking change.
If this is up for grabs, I'd take this.
The text was updated successfully, but these errors were encountered: