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

Number.isFinite should assert the value is number #116

Conversation

mitchell-merry
Copy link

The Number.isFinite() method determines whether the passed value is a finite number — that is, it checks that the type of a given value is Number, and the number is neither positive Infinity, negative Infinity, nor Na
  • JSDoc for isFinite

This PR changes isFinite to a type guard for number. Prevents workarounds:

function myFunction(value: unknown) {
  // typeof value === number is redundant here, but required for TS
  if (typeof value === "number" && Number.isFinite(value)) {
    console.log(value); // number
  }
}

Please be nitpicky about the README changes.

Also, the similar functions isInteger, isSafeInteger, and isNan (not sure about the last one) also have the same issue that could be fixed. I'm hesitant to add them all in one go, though.

@mattpocock
Copy link
Collaborator

I don't think this is useful, because .isFinite already requires the input to be a number.

@mattpocock mattpocock closed this Mar 20, 2023
@mitchell-merry
Copy link
Author

Hi Matt, not too sure what you mean. As far as I can tell, it allows unknown to be passed in:
https://github.com/microsoft/TypeScript/blob/main/src/lib/es2015.core.d.ts#L207

@mitchell-merry
Copy link
Author

But, I suppose this would introduce the issue where it would incorrectly narrow in the false case. microsoft/TypeScript#15048

@mattpocock
Copy link
Collaborator

@mitchell-merry Aha, you're right! Though yes, I am a bit concerned about introducing too many predicates into ts-reset.

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

Successfully merging this pull request may close these issues.

None yet

2 participants