メインコンテンツまでスキップ

bigint型(長整数型)

JavaScriptのbigint型は、number型よりも大きな整数を扱えるプリミティブ型です。

bigint型リテラル

JavaScriptのbigint型のリテラルは整数値の末尾にnをつけて書きます。

ts
const x = 100n;
ts
const x = 100n;

bigintリテラルをTypeScriptで用いるには、コンパイラーオプションのtargetをes2020以上にする必要があります。

bigint型の型注釈

TypeScriptでbigint型を型注釈するには、bigintを用います。

ts
const x: bigint = 100n;
ts
const x: bigint = 100n;

BigInt関数

bigint型はBigInt関数を使って作ることができます。BigInt関数は第1引数に数値もしくは文字列を渡します。

ts
const x = BigInt(100);
const y = BigInt("9007199254740991");
ts
const x = BigInt(100);
const y = BigInt("9007199254740991");

TypeScriptでBigInt関数を用いるには、コンパイラーオプションのlibをes2020以上にする必要があります。

bigint型をnumber型と計算する

bigint型とnumber型はそのままでは一緒に演算をすることはできません。どちらかに型を合わせる必要があります。

ts
2n + 3;
Operator '+' cannot be applied to types '2n' and '3'.2365Operator '+' cannot be applied to types '2n' and '3'.
ts
2n + 3;
Operator '+' cannot be applied to types '2n' and '3'.2365Operator '+' cannot be applied to types '2n' and '3'.

number型が小数部を持っていない限り、より表現幅の広いbigint型に合わせる方が無難です。

ts
const i = 2n + BigInt(3);
console.log(i);
5n
ts
const i = 2n + BigInt(3);
console.log(i);
5n