Skip to content

A beginner's cheat sheet for TypeScript

Published: at 12:00 AM✎ Suggest Changes

For a long time, I stayed away from TypeScript. I thought it was too verbose, too strict, or just something that added friction to my workflow. But once I gave it a real shot, I realized how much peace of mind it brings when working on bigger web projects.

This post is a quick primer for those who want to get started with TypeScript but don’t want to read through the whole manual. 📘 Instead, this cheat sheet highlights the essentials that helped me understand its mindset and syntax.

What TypeScript is (and what it’s not)

Before jumping in, let’s clarify a few things.

TypeScript is:

TypeScript is not:


🧠 Cheat Sheet: TypeScript Essentials

Here’s a quick reference to the things I use when writing TypeScript.

✨ Basic Types

let isDone: boolean = false;
let age: number = 33;
let username: string = "angel";
let tags: string[] = ["astro", "typescript"];
let anything: any = "you can assign anything here";

🧱 Tuples and Enums

let user: [string, number] = ["Angel", 33];

enum Status {
  Loading,
  Success,
  Error,
}

let current: Status = Status.Loading;

🔍 Type Aliases and Interfaces

type User = {
  name: string;
  age: number;
};

interface Product {
  id: number;
  name: string;
  price?: number; // optional
}

Both type and interface can be used for declaring object shapes. In general:

🧰 Functions with Types

function greet(name: string): string {
  return `Hello, ${name}`;
}

const sum = (a: number, b: number): number => a + b;

🧱 Union and Literal Types

let status: "loading" | "success" | "error";
status = "loading"; //
status = "done"; // ❌ Error

type ID = string | number;
let userId: ID = 123;

🏗️ Type Narrowing

function handleInput(input: string | number) {
  if (typeof input === "string") {
    console.log("String input:", input);
  } else {
    console.log("Number input:", input);
  }
}

🧩 Generics

function identity<T>(arg: T): T {
  return arg;
}
const str = identity<string>("Hello");
const num = identity<number>(42);

🦺 Type Guards and in

type Dog = { bark: () => void };
type Cat = { meow: () => void };

function isDog(animal: Dog | Cat): animal is Dog {
  return "bark" in animal;
}

Happy typing! ⌨️✨


Next Post
SOLID principles in R6 classes