-
Notifications
You must be signed in to change notification settings - Fork 506
fix(isFinite): implement type predicate to narrow type to number #1511
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 isFinite function to act as a TypeScript type guard, improving type safety and developer experience. The function now properly narrows the type of unknown values to number when they are finite.
- Updated function signature to use
value is numbertype predicate instead of returningboolean - Enhanced implementation to explicitly check
typeof value === 'number'before callingNumber.isFinite() - Updated documentation across multiple languages to explain the type guard behavior
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/compat/predicate/isFinite.ts | Changed return type to value is number and added explicit typeof check to properly guard against non-number types |
| src/compat/predicate/isFinite.spec.ts | Replaced strict type equality test with compatibility test and added new test for type predicate functionality |
| docs/reference/compat/predicate/isFinite.md | Updated return type documentation and added TypeScript type predicate explanation |
| docs/zh_hans/reference/compat/predicate/isFinite.md | Updated return type documentation and added TypeScript type predicate explanation (Chinese) |
| docs/ko/reference/compat/predicate/isFinite.md | Updated return type documentation and added TypeScript type predicate explanation (Korean) |
| docs/ja/reference/compat/predicate/isFinite.md | Updated return type documentation and added TypeScript type predicate explanation (Japanese) |
Comments suppressed due to low confidence (1)
docs/reference/compat/predicate/isFinite.md:49
- The parameter type in the documentation shows
anybut the actual function signature usesunknown. This should be updated tounknownto match the implementation.
- `value` (`any`): The value to check.
💡 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 so much!
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1511 +/- ##
=======================================
Coverage 99.97% 99.97%
=======================================
Files 468 468
Lines 4459 4459
Branches 1313 1313
=======================================
Hits 4458 4458
Misses 1 1 🚀 New features to boost your workflow:
|
Closed #1508
Description
This PR updates the
isFiniteutility 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 its intended behavior and enabling type narrowing for conditional checks.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 and stricter type inference.Documentation Update:
Modified MDX docs:
ParametersandReturnsto reflect the new type predicate signature.TypeScript noteexplaining type narrowing behavior with examples.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 implied that
isFinitecould act as a type guard,but it actually returned a plain boolean and didn’t affect type inference in TypeScript.
This update ensures the function truly works as a type predicate,
improving developer experience and aligning runtime behavior with TypeScript semantics.
Breaking Changes
Projects relying on an explicit
booleanreturn type may need to adjust dependent type definitions or mocks.