-
Notifications
You must be signed in to change notification settings - Fork 0
Description
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:
- https://github.com/bare-ts/lib/blob/main/src/codec/primitive.ts
- https://github.com/bare-ts/lib/blob/main/src/util/validator.ts
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:
- Update validators to handle both types:
export function isI64(val: bigint | number): boolean {
// ...
}
- 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.