Skip to content

Commit 477560b

Browse files
fix(compiler): returning negative bigints from asBigInit (#5295)
Co-authored-by: Will Temple <[email protected]>
1 parent ac41d8a commit 477560b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/compiler"
5+
---
6+
7+
Fix incorrectly returning a positive `BigInt` for a negative `Numeric`.

packages/compiler/src/core/numeric.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ const NumericPrototype = {
195195
return equals(this[InternalDataSym], Numeric(num.toString())[InternalDataSym]) ? num : null;
196196
},
197197
asBigInt: function (this: Numeric) {
198-
return this.isInteger ? this[InternalDataSym].n : null;
198+
if (!this.isInteger) {
199+
return null;
200+
}
201+
202+
const { s, n } = this[InternalDataSym];
203+
return BigInt(s) * n;
199204
},
200205
equals: function (this: Numeric, other: Numeric) {
201206
return equals(this[InternalDataSym], other[InternalDataSym]);

packages/compiler/test/core/numeric.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ describe("asNumber", () => {
215215
["123.456", 123.456],
216216
["123.00", 123],
217217
["123456789123456789123456789123456789", null],
218+
["-123456789123456789123456789123456789", null],
218219
["123456789123456789.123456789123456789", null],
219220
])("%s => %d", (a, b) => {
220221
const numeric = Numeric(a);
@@ -229,6 +230,7 @@ describe("asBigInt", () => {
229230
["123.456", null],
230231
["123.00", 123n],
231232
["123456789123456789123456789123456789", 123456789123456789123456789123456789n],
233+
["-123456789123456789123456789123456789", -123456789123456789123456789123456789n],
232234
["123456789123456789.123456789123456789", null],
233235
])("%s => %d", (a, b) => {
234236
const numeric = Numeric(a);

0 commit comments

Comments
 (0)