diff --git a/src/value/convert/convert.ts b/src/value/convert/convert.ts index 92e6c5811..c824d92a5 100644 --- a/src/value/convert/convert.ts +++ b/src/value/convert/convert.ts @@ -119,8 +119,9 @@ function TryConvertBoolean(value: unknown) { return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value } function TryConvertBigInt(value: unknown) { + value = IsNumber(value) ? value.toString() : value; const truncateInteger = (value: string) => value.split('.')[0] - return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsNumber(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value + return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value } function TryConvertString(value: unknown) { return IsValueToString(value) ? value.toString() : IsSymbol(value) && value.description !== undefined ? value.description.toString() : value diff --git a/test/runtime/value/convert/bigint.ts b/test/runtime/value/convert/bigint.ts index 383e692a6..40d5b30d5 100644 --- a/test/runtime/value/convert/bigint.ts +++ b/test/runtime/value/convert/bigint.ts @@ -53,6 +53,16 @@ describe('value/convert/BigInt', () => { const R = Value.Convert(T, 3.14) Assert.IsEqual(R, BigInt(3)) }) + it('Should convert bigint from number 3', () => { + const T = Type.BigInt() + const R = Value.Convert(T, 2147483648) + Assert.IsEqual(R, BigInt(2147483648)) + }) + it('Should convert bigint from number 4', () => { + const T = Type.BigInt() + const R = Value.Convert(T, Number.MAX_SAFE_INTEGER) + Assert.IsEqual(R, BigInt(9007199254740991)) + }) it('Should convert bitint from boolean 1', () => { const T = Type.BigInt() const R = Value.Convert(T, true)