ProgramGuru

The bigint and symbol Types

Learn The bigint and symbol Types

bigint represents arbitrary-size integers, while symbol creates unique property keys or identifiers.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind the bigint and symbol types.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Write a bigint literal with n, such as 100n. Bigints cannot be mixed directly with number arithmetic. Symbol("id") creates a unique symbol even if another symbol uses the same description. These types exist at JavaScript runtime, not just in TypeScript type declarations.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const huge: bigint = 9007199254740993n;
 const doubled = huge * 2n;
 const keyA: symbol = Symbol("id");
 const keyB: symbol = Symbol("id");
 console.log(doubled.toString());
 console.log(keyA === keyB);

Verified output

18014398509481986
false

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The bigint multiplication remains exact and toString prints its decimal digits. The two separately created symbols are distinct, so equality is false.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not add a bigint to a number without converting intentionally. JSON.stringify also does not serialize bigint values by default.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Change doubled to huge + 1n, then compare Symbol.for("shared") values with ordinary Symbol calls.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What is true about Symbol("id") === Symbol("id")?
Choose one answer. You can try again.

Answer and explanation

It is false for two newly created symbols.

Every ordinary Symbol call creates a distinct symbol.

Review the key idea

The bigint and symbol Types: Write a bigint literal with n, such as 100n.

Every ordinary Symbol call creates a distinct symbol.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: The any Type →