typescript-plugin-bigint
v0.0.0
Published
A TypeScript transformer that converts BigInt operator to library calls
Downloads
3
Maintainers
Readme
TypeScript Plugin BigInt
A TypeScript transformer that converts BigInt operators to library calls.
Features
- Replaces all addition operators (
+) withaddfunction calls - Automatically imports the
addfunction from thebigint-runtimelibrary - Avoids duplicate imports, only adding import statements when needed
- Supports correct conversion of nested expressions
- Easily integrates into existing TypeScript projects
- Provides complete type definitions
Installation
# Install the plugin
pnpm i -D typescript-plugin-bigint
# Install runtime dependency
pnpm i bigint-runtimeUsage
With Rollup
// rollup.config.js
const typescript = require('@rollup/plugin-typescript');
const bigIntPlugin = require('typescript-plugin-bigint').default;
module.exports = {
// ...other options
plugins: [
typescript({
transformers: (program) => ({
before: [
bigIntPlugin(program)
]
})
})
]
};With webpack's ts-loader
In your webpack configuration:
const bigIntPlugin = require('typescript-plugin-bigint').default;
module.exports = {
// ...other configuration
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
getCustomTransformers: (program) => ({
before: [
bigIntPlugin(program)
]
})
}
}
]
}
};With ts-patch/ttypescript
{
"compilerOptions": {
"plugins": [
{ "transform": "typescript-plugin-bigint" }
]
}
}Transformation Examples
Before
export function add(a: bigint, b: bigint) {
return a + b;
}After
import * as JSBI from "bigint-runtime";
export function add(a: bigint, b: bigint) {
return JSBI.add(a, b);
}Supported Operations
The plugin transforms various BigInt operations to their corresponding library calls:
Literal Transformations
| Operation | Before | After |
| -------------------- | ----------------------- | --------------------------------- |
| literals | a = 123n; | JSBI.BigInt(123) |
| literals | a = 9007199254740993n | JSBI.BigInt('9007199254740993') |
| Creation from String | a = BigInt('456') | a = JSBI.BigInt('456') |
| Creation from Number | a = BigInt(789) | a = JSBI.BigInt(789) |
| Conversion to String | a.toString(radix) | a.toString(radix) |
| Conversion to Number | Number(a) | JSBI.toNumber(a) |
| Truncation | BigInt.asIntN(64, a) | JSBI.asIntN(64, a) |
| | BigInt.asUintN(64, a) | JSBI.asUintN(64, a) |
BigInt Operations
| Operation | Before | After |
| --------------------------- | ------------ | --------------------------------- |
| Addition | c = a + b | c = JSBI.add(a, b) |
| Subtraction | c = a - b | c = JSBI.subtract(a, b) |
| Multiplication | c = a * b | c = JSBI.multiply(a, b) |
| Division | c = a / b | c = JSBI.divide(a, b) |
| Remainder | c = a % b | c = JSBI.remainder(a, b) |
| Exponentiation | c = a ** b | c = JSBI.exponentiate(a, b) |
| Negation | b = -a | b = JSBI.unaryMinus(a) |
| Bitwise negation | b = ~a | b = JSBI.bitwiseNot(a) |
| Left shifting | c = a << b | c = JSBI.leftShift(a, b) |
| Right shifting | c = a >> b | c = JSBI.signedRightShift(a, b) |
| Bitwise "and" | c = a & b | c = JSBI.bitwiseAnd(a, b) |
| Bitwise "or" | c = a \| b | c = JSBI.bitwiseOr(a, b) |
| Bitwise "xor" | c = a ^ b | c = JSBI.bitwiseXor(a, b) |
| Comparison to other BigInts | a === b | JSBI.equal(a, b) |
| | a !== b | JSBI.notEqual(a, b) |
| | a < b | JSBI.lessThan(a, b) |
| | a <= b | JSBI.lessThanOrEqual(a, b) |
| | a > b | JSBI.greaterThan(a, b) |
| | a >= b | JSBI.greaterThanOrEqual(a, b) |
Mixed-type Operations
| Operation | Before | After |
| ------------------------------- | -------- | ---------------- |
| Abstract equality comparison | x == y | JSBI.EQ(x, y) |
| Generic "not equal" | x != y | JSBI.NE(x, y) |
| Generic "less than" | x < y | JSBI.LT(x, y) |
| Generic "less than or equal" | x <= y | JSBI.LE(x, y) |
| Generic "greater than" | x > y | JSBI.GT(x, y) |
| Generic "greater than or equal" | x >= y | JSBI.GE(x, y) |
| Generic addition | x + y | JSBI.ADD(x, y) |
Partially Supported Operations
Some operations have limited support:
| Operation | Before | After |
| ----------- | ----------------------- | ------------------ |
| Type check | typeof a === "bigint" | JSBI.isBigInt(a) |
| Type check | typeof a | JSBI.isObject(a) |
| Type check | typeof a | JSBI.typeOf(a) |
| increment | a++ | N/A ☹ |
| decrement | a-- | N/A ☹ |
| increment | ++a | N/A ☹ |
| decrement | --a | N/A ☹ |
| assignment | a += 1n | N/A ☹ |
| assignment | a -= 1n | N/A ☹ |
| assignment | a *= 1n | N/A ☹ |
| assignment | a /= 1n | N/A ☹ |
| 0n is falsy | 0n ? 1 : 0 | N/A ☹ |
