dcodeIO / long.js

A Long class for representing a 64-bit two's-complement integer value.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is the heigh argument in Long constructor required?

webmaster128 opened this issue · comments

The type definitions in @types/long define a type constructor like this:

new( low: number, high?: number, unsigned?: boolean ): Long;

The optional height allows users to write new Long(123). This is a problem as it makes it way too easy for callers to assume this is the correct number to Long conversion because it works for a very long time until it breaks.

The README marks heigh as required. The implementation works fine if unset because undefined | 0 is 0.

Is it safe to assume heigh is required and this can be considered a bug in the types?

commented

I don't think this is a bug. The official types shipped with the library define:

long.js/index.d.ts

Lines 7 to 10 in ce11b4b

/**
* Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as signed integers. See the from* functions below for more convenient ways of constructing Longs.
*/
constructor(low: number, high?: number, unsigned?: boolean);

The comment states that provided values must be 32-bit. The correct way to construct a Long from a number is Long.fromNumber:

long.js/index.d.ts

Lines 77 to 80 in ce11b4b

/**
* Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
*/
static fromNumber(value: number, unsigned?: boolean): Long;

The README, however, appears to be out of date, and high should be optional there.

Thanks for the detailed answer and README fix!