Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 9

Primitive Types & Type Annotations

TypeScript's primitive types map directly to JavaScript's: `string`, `number`, `boolean`, `null`, `undefined`, `symbol`, and `bigint`. Type annotations use a colon after the variable name. TypeScript is smart enough to infer most types automatically - you only need to annotate when inference can't do the job.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Primitives - Annotated & Inferred

Primitives - Annotated & Inferred
// ── Explicit annotations ──────────────────────────────────────
const name: string    = "Alice";
const age: number     = 30;
const active: boolean = true;
const score: bigint   = 9007199254740993n;
const id: symbol      = Symbol("userId");

// ── Null and undefined - enabled by strict mode ────────────────
// With strict: true, null and undefined are NOT assignable to
// other types unless you explicitly allow them.
const email: string | null      = null;      // username not set yet
const birthday: Date | undefined = undefined; // optional field

// ── Type inference - let TypeScript figure it out ──────────────
const city = "London";      // TypeScript infers: string
const count = 0;            // TypeScript infers: number
const flags = [true, false]; // TypeScript infers: boolean[]

// When inference works - don't annotate (it's redundant noise):
// ❌ const message: string = "Hello"; // annotation is redundant
// ✅ const message = "Hello";         // inference is sufficient

// When to annotate:
// ✅ Function parameters (inference can't read minds)
// ✅ Variables initialised as null/undefined
// ✅ Return types of public API functions
// ✅ Class properties declared without initialisation

// ── Special types ──────────────────────────────────────────────
// object - non-primitive (avoid: too broad)
const config: object = { debug: true };     // ❌ can't access .debug

// better: use an interface or type alias
const settings: { debug: boolean } = { debug: true };
console.log(settings.debug);                // ✅

// void - function returns nothing (not the same as undefined)
function logMessage(msg: string): void {
  console.log(msg);
  // no return needed
}

// undefined - explicitly returns undefined
function findUser(id: number): string | undefined {
  if (id === 1) return "Alice";
  return undefined;
}

Common Mistakes

  • Using `object` as a type - it's legal but almost useless because you can't access any properties. Use a specific interface instead.
  • Thinking `void` and `undefined` are the same - `void` means the return value should not be used. A function typed `void` can technically return `undefined`, but callers should not use the return value.
  • Annotating every variable - TypeScript's inference is excellent. Over-annotating makes code verbose. Annotate at boundaries (function params, class props, API return types), not everywhere.

Tip

Tip

Practice Primitive Types Type Annotations in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Primitive Types & Type Annotations

Practice Task

Note

Practice Task - (1) Write a working example of Primitive Types Type Annotations from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Primitive Types Type Annotations is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • TypeScript's primitive types map directly to JavaScript's: `string`, `number`, `boolean`, `null`, `undefined`, `symbol`, and `bigint`.
  • Using `object` as a type - it's legal but almost useless because you can't access any properties. Use a specific interface instead.
  • Thinking `void` and `undefined` are the same - `void` means the return value should not be used. A function typed `void` can technically return `undefined`, but callers should not use the return value.
  • Annotating every variable - TypeScript's inference is excellent. Over-annotating makes code verbose. Annotate at boundaries (function params, class props, API return types), not everywhere.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done