TypeScript Types — Everything at a Glance



This content originally appeared on DEV Community and was authored by joyonto kumar roy

TypeScript is a layer on top of JavaScript that brings type safety to our code. This helps catch errors early and makes the code easier to read and maintain.

In today’s article, we’ll dive into all the important types in TypeScript. We’ll provide examples based on real-life scenarios so that both beginners and experienced developers can benefit.

We will divide the types into three main categories:

  1. Primitive Types
  2. Special Types
  3. Complex Types

Primitive Types:

✅ string holds text or string values.
let username: string = "Sadia";
Use cases: user name, messages, titles, etc.

✅ number holds whole numbers or decimal values.
let price: number = 299.99;
let age: number = 25;
Use cases: age, price, quantity, etc.

✅ boolean holds a true or false value.
let isLoggedIn: boolean = true;
Use cases: login status, condition checks, etc.

✅ bigint is used to store very large numbers.
let population: bigint = 9007199254740991n;
Use cases: large data like bank account numbers.

✅ symbol is always unique. Used as unique keys in objects.
let id: symbol = Symbol("id");
Use cases: unique properties or secret keys.

✅ undefined means a variable has been declared but not assigned any value.
let result: number | undefined;
Use cases: variable is declared but still holds no value.

✅ null is used to intentionally indicate "no value".
let selectedUser: string | null = null;
Use cases: when you want to explicitly say "no value".

Special Types:

✅ any can hold values of any type and turns off type checking.
let data: any = "Hello";
data = 123;
Use case: When the type is unknown, but should be used with caution.

✅ unknown can hold any type like any, but you must check the type before using it.
let input: unknown = "Text";
if (typeof input === "string") {
  console.log(input.toUpperCase());
}
Use case: A safer alternative to any when handling unknown data.

✅ void is used when a function doesn’t return anything.
function logMessage(message: string): void {
  console.log(message);
}
Use case: For functions that perform an action but don’t return a value.

✅ never is used when a function never returns (e.g., throws an error or has an infinite loop).
function throwError(): never {
  throw new Error("Something went wrong!");
}
Use case: For functions that never finish or always fail.

Complex Types:

✅ array stores multiple values of the same type.
let numbers: number[] = [1, 2, 3];
let fruits: string[] = ["apple", "banana"];

✅ tuple stores a fixed number of values of different types.
let user: [string, number] = ["Alice", 30];
Use case: When you need to store mixed-type values in a fixed order.

✅ object stores data as key-value pairs.
let person: { name: string; age: number } = {
  name: "Tanvir",
  age: 27,
};

✅ function can have defined parameter and return types.
function add(a: number, b: number): number {
  return a + b;
}

✅ union allows a variable to be one of multiple types.
let value: string | number = "Hello";
value = 42;

✅ intersection combines multiple types into one, requiring all their properties.
type Admin = { role: string };
type User = { name: string };
type AdminUser = Admin & User;

const admin: AdminUser = {
  role: "superadmin",
  name: "Karim",
};

✅ literal allows a variable to have specific, fixed values.
let status: "success" | "error" = "success";

✅ enum creates a named set of constant values.
enum Direction {
  Up,
  Down,
  Left,
  Right,
}

let move: Direction = Direction.Up;

In the next part, we will dive deep into all the important TypeScript types that will help you learn TypeScript more thoroughly. We will explore the usage of Primitive, Union, Intersection, Tuple, Enum, Interface, Generics, and Utility Types with real-world examples and detailed explanations.


This content originally appeared on DEV Community and was authored by joyonto kumar roy