TypeError: can't convert BigInt to number
Ngoại lệ JavaScript "can't convert BigInt to number" xảy ra khi một phép tính số học liên quan đến hỗn hợp các giá trị BigInt và Number.
Thông báo
TypeError: Cannot convert a BigInt value to a number (V8-based) TypeError: Cannot mix BigInt and other types, use explicit conversions (V8-based) TypeError: BigInts have no unsigned right shift, use >> instead (V8-based) TypeError: can't convert BigInt to number (Firefox) TypeError: Conversion from 'BigInt' to 'number' is not allowed. (Safari) TypeError: Invalid mix of BigInt and other type in addition/multiplication/…. (Safari) TypeError: BigInt does not support >>> operator (Safari)
Loại lỗi
Nguyên nhân?
Hai vế của toán tử số học phải đều là BigInts hoặc đều không phải. Nếu một phép tính liên quan đến hỗn hợp BigInts và numbers, không rõ kết quả nên là BigInt hay number, vì có thể mất độ chính xác trong cả hai trường hợp.
Lỗi cũng xảy ra khi một BigInt được chuyển đổi ngầm thành number thông qua quá trình ép kiểu number. Ví dụ, nếu một BigInt được truyền vào một phương thức tích hợp mong đợi một number.
Lỗi cũng có thể xảy ra nếu toán tử unsigned right shift (>>>) được sử dụng giữa hai BigInts. Trong Firefox, thông báo là giống nhau: "can't convert BigInt to number".
Ví dụ
>Hỗn hợp numbers và BigInts trong các phép tính
const sum = 1n + 1;
// TypeError: can't convert BigInt to number
Thay vào đó, hãy chuyển đổi rõ ràng một vế thành BigInt hoặc number.
const sum = 1n + BigInt(1);
const sum2 = Number(1n) + 1;
Sử dụng unsigned right shift trên BigInts
const a = 4n >>> 2n;
// TypeError: can't convert BigInt to number
Hãy dùng right shift thông thường thay thế.
const a = 4n >> 2n;