Course contentsShow
← TypeScript
Lesson 89 of 1,7963. Basic TypesFree lesson

The bigint Type

Discover how bigint handles arbitrarily large integers beyond number's safe range.

The bigint Type

What you'll learn: How TypeScript's bigint type lets you work with integers larger than JavaScript's number can safely represent.

Why bigint Exists

Remember learning about the number type? It handles integers and decimals, but it has a hidden limitation: it can only safely represent integers between -9007199254740991 and 9007199254740991 (about ±9 quadrillion). Beyond that range, precision breaks down and math becomes unreliable.

Think of number like a calculator with a fixed-size display—once you exceed its digit limit, things get weird. The bigint type is like upgrading to a calculator with infinite digits.

How bigint Works

In TypeScript, you create a bigint by appending n to an integer literal:

const maxSafeNumber: number = 9007199254740991;
const beyondSafe: bigint = 9007199254740992n;

const hugeValue: bigint = 123456789012345678901234567890n;

Notice the n suffix? That tells TypeScript (and JavaScript) you're working with a bigint, not a regular number.

Important Boundaries

You cannot mix number and bigint in arithmetic without explicit conversion:

const a: number = 100;
const b: bigint = 50n;

// This won't compile:
// const sum = a + b;  // Error!

They're separate types for safety—TypeScript prevents accidental mixing that could cause precision loss.

Key Takeaway: Use bigint when you need integers beyond number's safe range (±9 quadrillion), marking them with the n suffix to keep them separate from regular numbers.