Skip to content

Resolve Type Inconsistencies in 64-bit Validators and Write Functions #1

@merlindiavova

Description

@merlindiavova

Problem Summary

The validator functions currently require bigint types, but in practice, I (and other developers) often pass number values to write functions like writeI64 and writeU64. This mismatch causes runtime errors and adds unnecessary friction.

Affected files:

Why This Matters

I would like the API to be intuitive and consistent. Right now, it's too easy to hit a runtime error when using numbers instead of BigInts, especially since we naturally reach for numbers for smaller values.

Proposed Solution: Dual-Type Support

Updating the validators and write functions to accept both bigint and number types. This approach reduces surprises and keeps the API simple and flexible.

Changes:

  1. Update validators to handle both types:
export function isI64(val: bigint | number): boolean {
  // ...
}
  1. Modify write functions to auto-convert numbers to BigInt:
export function writeU64(bc: ByteCursor, x: bigint | number): void {
  const bigIntValue = typeof x === 'bigint' ? x : BigInt(x);
  // ...
}

Additional Considerations

Documentation
Clearly document that writeI64/writeU64 now support both types, and reserve Safe versions for cases where number-only input is explicitly desired.

Testing
Add tests for mixed-type inputs to ensure stability across use cases.

Impact

  • Breaking change? Mild, expanding signatures, not restricting them.
  • Backward compatibility? Preserved, existing BigInt usage remains unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions