Skip to content

Date.getMonth could return a union of all possible values #817

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

Closed
nikeee opened this issue Jan 21, 2020 · 1 comment
Closed

Date.getMonth could return a union of all possible values #817

nikeee opened this issue Jan 21, 2020 · 1 comment

Comments

@nikeee
Copy link
Contributor

nikeee commented Jan 21, 2020

According to the MDN docs, Date.getMonth() always returns a number between 0 and 11 (inclusive):

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.

@nikeee 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
@nikeee
Copy link
Contributor Author

nikeee commented Jan 24, 2020

I think this issue actually belongs to the typescript main repository: microsoft/TypeScript#36401.

@nikeee nikeee closed this as completed Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant