DEV Community

Jeferson Eiji
Jeferson Eiji

Posted on • Originally published at dev.to

Understanding Primitive Types in TypeScript: A Quick Guide

TypeScript provides a set of primitive types that form the foundation for building type-safe applications. Knowing these types helps you write cleaner, more predictable code.

List of Primitive Types in TypeScript

  • string: Represents textual data.
    • Example: let name: string = "Alice";
  • number: For all numbers, including integers and floats.
    • Example: let age: number = 30;
  • boolean: Represents true or false values.
    • Example: let isActive: boolean = true;
  • null: Indicates the intentional absence of any value.
    • Example: let data: null = null;
  • undefined: Represents an uninitialized variable.
    • Example: let count: undefined = undefined;
  • symbol: Used to create unique identifiers (ES6+ feature).
    • Example: let id: symbol = Symbol('id');
  • bigint: For working with large integers beyond the safe integer limit for numbers (ES2020+ feature).
    • Example: let bigNumber: bigint = 9007199254740991n;

Why Use Primitive Types?

  • They ensure type safety
  • Help prevent runtime errors
  • Make your code self-documenting

Example Usage

let title: string = "TypeScript";
let year: number = 2024;
let enrolled: boolean = false;
Enter fullscreen mode Exit fullscreen mode

Understanding and using these primitive types is a core part of writing robust TypeScript code.

Top comments (0)