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

Extend "no comparison with NaN" check to cover "Number.NaN" #52780

Open
nevedaren opened this issue Feb 15, 2023 · 4 comments
Open

Extend "no comparison with NaN" check to cover "Number.NaN" #52780

nevedaren opened this issue Feb 15, 2023 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@nevedaren
Copy link

Suggestion

πŸ” Search Terms

x === Number.NaN
disallow comparing with Number.NaN

βœ… Viability Checklist

My suggestion meets these guidelines:

  • [?] This wouldn't be a breaking change in existing TypeScript/JavaScript code - this would break code containing the error
  • βœ… This wouldn't change the runtime behavior of existing JavaScript code
  • βœ… This could be implemented without emitting different JS based on the types of the expressions
  • βœ… This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • βœ… This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Extend check added for #49962 to include Number.NaN in addition to global NaN.

πŸ“ƒ Motivating Example

function useVersionNumber(value: string) {
    let versionNumber = Number(value);
    if (versionNumber === Number.NaN) {
        return;
    }
    console.log(versionNumber + 1);
}

πŸ’» Use Cases

Catching a developer error (incorrect NaN handling).

@RyanCavanaugh
Copy link
Member

Why do people use Number.NaN ?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 15, 2023
@nevedaren
Copy link
Author

I don't know the exact reasoning behind it. In the cases I saw, NaN could be used instead. Maybe people wanted to be more explicit or didn't know they could just use the global NaN.

However, it is definitely used sometimes. #50626 covered majority of our cases (thanks a-tarasyuk and everyone involved!), but we also saw some rare cases of comparison with Number.NaN - e.g. I took the example attached from an existing code.

I understand it's a rare scenario, but it still might happen and the check could prevent some breakages. Would it be hard to cover this?

@RyanCavanaugh
Copy link
Member

Not difficult, but everything costs a tiny bit of performance and it really does add up over time.

@fatcerberus
Copy link

To be fair, it seems it'd only be a perf issue because it requires a new special case - since the check already exists for the global NaN, a NaN literal type would likely solve this elegantly in the general case without need for further special cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants