Skip to content

Suggestion: More narrow type for Math.sign #19954

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
kreba opened this issue Nov 12, 2017 · 3 comments
Closed

Suggestion: More narrow type for Math.sign #19954

kreba opened this issue Nov 12, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@kreba
Copy link

kreba commented Nov 12, 2017

The type for Math.sign is currently (x: number): number as seen in
https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts#L4177

Its type could be (x: number): -1 | 0 | 1 instead.

That would allow for more specific typing in app code without having to wrap Math.sign.

@aluanhaddad
Copy link
Contributor

This would be great but

Math.sign(NaN) // NaN
typeof NaN // "number"

which poisons what would otherwise be quite elegant behavior.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 13, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2017

discussed previously in #15356

@kreba
Copy link
Author

kreba commented Nov 14, 2017

Thank you for your immediate response!

I had never considered what happens with NaN. Now I understand why it's return type is currently number.

I'll continue wrapping it for my comparators then.

@kreba kreba closed this as completed Nov 14, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants