-
Notifications
You must be signed in to change notification settings - Fork 506
fix(isSafeInteger): implement type predicate to narrow type to number #1510
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the isSafeInteger function to use TypeScript's type predicate feature, enabling better type narrowing. The function signature is updated from returning boolean to returning value is number, and the parameter type is improved from any to unknown.
Key Changes
- Updated function signature to use TypeScript type predicate (
value is number) - Changed parameter type from
anytounknownfor better type safety - Updated JSDoc comments and documentation across all language versions to reflect the type predicate behavior
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/compat/predicate/isSafeInteger.ts | Updated function signature to use type predicate and improved parameter type from any to unknown |
| docs/reference/compat/predicate/isSafeInteger.md | Added documentation explaining the type predicate behavior in English |
| docs/zh_hans/reference/compat/predicate/isSafeInteger.md | Added documentation explaining the type predicate behavior in Chinese |
| docs/ko/reference/compat/predicate/isSafeInteger.md | Added documentation explaining the type predicate behavior in Korean |
| docs/ja/reference/compat/predicate/isSafeInteger.md | Added documentation explaining the type predicate behavior in Japanese |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
raon0211
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Description
This PR updates the
isSafeIntegerutility to behave as a proper TypeScript type predicate.Previously, it only returned a
booleanvalue, which did not narrow the input type.Now it returns
value is number, aligning with the JSDoc description and enabling type narrowing in conditional branches.Changes
Type Predicate Update (major fix):
Updated the return type from
boolean→value is number.This allows TypeScript to narrow the argument type to
numberwhen the function returnstrue.Parameter Type Refinement:
Changed the parameter type from
any→unknownfor safer type inference.Documentation Update:
Modified MDX docs:
ParametersandReturnstable to reflect the new type predicate.TypeScript notesection explaining type narrowing behavior.Type Test Update:
Added a dedicated type predicate test case verifying that the value is narrowed to
numberinside conditional blocks.Motivation
The previous implementation’s JSDoc claimed that it could “serve as a type predicate,”
but in reality, it didn’t provide any type narrowing because the function signature returned a plain boolean.
This update ensures that the function now truly behaves as a type guard, matching both runtime behavior and TypeScript semantics.
Breaking Changes
Projects relying on an explicit
booleanreturn type may need to update dependent type definitions or mocks.