BigInt support has been added on TypeScript 3.2; make sure your version is compatible.
But on top of that, you need to decide how BigInt will be supported on the context of your script - will you provide polyfills, or will you only run your script on environments that are guaranteed to have BigInt support?
This means you will need esnext as your build target (likely on tsconfig.json's target field), since BigInt is not compatible with previous ECMAScript versions.
If you do include a BigInt polyfill, you can use esnext.bigint as part of the lib field during transpilation. This adds the needed definitions to the process.
BigInt support has been added on TypeScript 3.2; make sure your version is compatible.
But on top of that, you need to decide how BigInt will be supported on the context of your script - will you provide polyfills, or will you only run your script on environments that are guaranteed to have BigInt support?
This means you will need esnext as your build target (likely on tsconfig.json's target field), since BigInt is not compatible with previous ECMAScript versions.
If you do include a BigInt polyfill, you can use esnext.bigint as part of the lib field during transpilation. This adds the needed definitions to the process.
It works for me to add esnext.bigint in lib .
{
"compilerOptions": {
"lib": ["es6","esnext.bigint"]
}
}
Typescript: using BigInt instead of number
TypeScript BigInt support
How do I add a (number | bigint) to another (number | bigint), enforcing they they are of the same type?
Support for TC39 "BigInt: Arbitrary precision integers in JavaScript" proposal
Videos
I have two variables "a" and "b." Both can either be a "number" or a "bigint" but they will be of the same type. How do I assure TypeScript they will be the same type?
MDN doc
BigIntis a built-in object that provides a way to represent whole numbers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with theNumberprimitive and represented by theNumber.MAX_SAFE_INTEGERconstant.BigIntcan be used for arbitrarily large integers.
Difference:
BigIntcannot be used with methods in the built-inMathobject and cannot be mixed with instances ofNumberin operations- Because coercing between
NumberandBigIntcan lead to loss of precision, it is recommended to only useBigIntwhen values greater than 2^53 are reasonably expected and not to coerce between the two types.
The differences between BigInt and Number:
Number |
BigInt |
|
|---|---|---|
| Safe Range | Number.MAX_SAFE_INTEGER; loses precision outside of this range |
Extremely large integers |
| Math operations | Supports the Math object |
Basic arithmetic operations: + * - % ** |
| Decimal vs. Integer support | Integers and decimals, e.g 1.00, 2.56 |
Only integers, e.g 1n, 2n. 5n / 2 === 2n |
| Syntax | No special syntax | Append n to the end of a number literal or use new BigInt('100') |
| Type Conversion | Automatically converts Number to BigInt in operations |
Requires explicit conversion to Number for arithmetic involving Number values |
| JSON | Supported by default | Not serializable by default |
Use BigInt when you need to work with extremely large integers or require precise integer arithmetic without any loss of precision.