Skip to content

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

Open
@nevedaren

Description

@nevedaren

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions