Skip to content

Conversation

@Beutife
Copy link

@Beutife Beutife commented Oct 28, 2025

Motivation

Remove usage of Number() constructor which is unsafe because:

  • It accepts any as parameter, allowing invalid types without TypeScript errors
  • Silently produces NaN when given invalid input
  • Makes code prone to runtime errors

Description

Added

  • bigintToNumber() utility function in @lodestar/utils
    • Validates bigint is within safe integer range
    • Throws descriptive errors instead of returning NaN
    • Provides compile-time type safety

Modified

  • Replaced Number(bigint) with bigintToNumber(bigint)
  • Replaced Number(string) with parseInt(string, 10)
  • Fixed code formatting across multiple files

Closes #issue_number
#7771

Steps to test or reproduce

git checkout fix-number-constructor-clean
yarn install
yarn build
yarn lint

@Beutife Beutife requested a review from a team as a code owner October 28, 2025 21:24
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Beutife, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the codebase to eliminate the unsafe usage of the Number() constructor, which can lead to silent NaN values and runtime errors due to its loose type acceptance. By introducing a new bigintToNumber() utility function and consistently using parseInt() for string conversions, the changes significantly improve type safety, error handling, and overall code robustness, making the system more predictable and less prone to unexpected numerical issues.

Highlights

  • New Utility Function: Introduced bigintToNumber() in @lodestar/utils for safe conversion of bigint to number, including validation for safe integer range and error throwing for invalid conversions.
  • Type-Safe BigInt Conversion: Replaced direct Number(bigint) calls with the new bigintToNumber() utility across the codebase to enhance type safety and prevent silent NaN issues.
  • Type-Safe String Conversion: Replaced Number(string) calls with parseInt(string, 10) to ensure explicit base conversion and safer handling of string-to-number parsing.
  • Code Formatting: Applied consistent code formatting across multiple files, improving readability and maintainability.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great step towards improving type safety by replacing the unsafe Number() constructor. The introduction of bigintToNumber for bigint conversions and the switch to parseInt for strings are excellent changes. My review focuses on ensuring the new parsing logic is as robust as possible. I've pointed out several instances where parseInt is used without a radix, which can lead to unexpected behavior. I've also noted a few minor code style issues. Overall, this is a valuable refactoring that enhances code quality.

return {
rootHex,
epoch: Number(epoch),
epoch: parseInt(epoch),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's a good practice to always specify the radix for parseInt. Since epoch is a decimal number string, you should specify radix 10 to ensure correct parsing and avoid potential issues.

    epoch: parseInt(epoch, 10),

Beutife and others added 9 commits October 28, 2025 22:29
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Oct 29, 2025

Codecov Report

❌ Patch coverage is 45.58824% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.91%. Comparing base (13fb933) to head (137f8a5).

Additional details and impacted files
@@            Coverage Diff            @@
##           unstable    #8584   +/-   ##
=========================================
  Coverage     51.90%   51.91%           
=========================================
  Files           848      849    +1     
  Lines         65797    65817   +20     
  Branches       4795     4797    +2     
=========================================
+ Hits          34153    34166   +13     
- Misses        31575    31582    +7     
  Partials         69       69           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant