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

isNaN accept all types of arguments #3947

Closed
wants to merge 1 commit into from

Conversation

dcharbonnier
Copy link

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN the argument can be a boolean, a string, a number or an object.

@msftclas
Copy link

Hi @dcharbonnier, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!

This seems like a small (but important) contribution, so no Contribution License Agreement is required at this point. Real humans will now evaluate your PR.

TTYL, MSBOT;

@@ -30,9 +30,9 @@ declare function parseFloat(string: string): number;

/**
* Returns a Boolean value that indicates whether a value is the reserved value NaN (not a number).
* @param number A numeric value.
* @param number A numeric or a string value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"or string"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about booleans, as you originally wrote?

@DanielRosenwasser
Copy link
Member

On one hand, I feel like this would fail to catch bugs, and if one really needs, they can always use a type guard (i.e. typeof a === "number" && isNaN(a)). On the other, the function is more general in its inputs, so this is a valid change.

@RyanCavanaugh
Copy link
Member

I'm struggling with utility of this. isNaN is just going to coerce the argument to a number, so writing isNaN(+x) is always going to be clearer in intent than isNaN(x) for non-number types of x.

Other function like Math.pow work the same way (e.g. Math.pow("3", " 5 ")) but aren't typed as accepting any type.

@dcharbonnier
Copy link
Author

Reading more the ecmascript doc the argument is specified as Number. String is a 'free' implementation of the specs.
Sorry for the noise

@DanielRosenwasser
Copy link
Member

No worries - always worth discussing!

@darrylring
Copy link

According to the ES5.1 specification, the isNaN and isFinite functions use the ToNumber abstract operation on the argument and return true if that result coerces to NaN or ±Infinity. To me, this implies that the argument to these functions can be any type. I think TypeScript should follow this as well.

Why check if a variable is a number before checking if it's not a number (NaN)? Why manually coerce a variable into a number before calling a function that, according to the specification, does just that?

Edit: I just created issue #4002.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants