This website requires JavaScript.
📦 Types
let age: number = 25;
let name: string = "Alice";
let isActive: boolean = true;
let anything: any = "Could be anything";
let nothing: null = null;
let notDefined: undefined = undefined;
🧮 Arrays & Tuples
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ["Alice", "Bob"];
let tuple: [string, number] = ["Age", 30];
🎯 Enums
enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}
let move: Direction = Direction.Up;
🛠 Functions
function add(a: number, b: number): number {
  return a + b;
}

const multiply = (a: number, b: number): number => a * b;

function greet(name: string, greeting: string = "Hello"): void {
  console.log(`${greeting}, ${name}`);
}

function optionalParam(a: number, b?: number): number {
  return b ? a + b : a;
}
🏗 Interfaces & Types
interface User {
  id: number;
  name: string;
  email?: string; // optional
}

type Product = {
  id: number;
  name: string;
  price: number;
};

const user: User = { id: 1, name: "Alice" };
🧱 Classes
class Person {
  constructor(public name: string, private age: number) {}

  greet() {
    console.log(`Hello, my name is ${this.name}`);
  }
}

const p1 = new Person("Bob", 25);
🧩 Generics
function identity<T>(value: T): T {
  return value;
}

let num = identity<number>(10);
let str = identity("Hello"); // type inferred

interface ApiResponse<T> {
  data: T;
  error?: string;
}
🧰 Utility Types
interface User {
  id: number;
  name: string;
  email: string;
}

type PartialUser = Partial<User>;   // All optional
type ReadonlyUser = Readonly<User>; // Immutable
type PickUser = Pick<User, "id" | "name">;
type OmitUser = Omit<User, "email">;
type UserKeys = keyof User;         // "id" | "name" | "email"
🔍 Type Narrowing
function printLength(x: string | string[]) {
  if (typeof x === "string") {
    console.log(x.length);
  } else {
    console.log(x.length);
  }
}
🛡 Type Assertions & Casting
let someValue: unknown = "Hello TS";
let strLength: number = (someValue as string).length;
⚙️ Modules
// file: math.ts
export function add(a: number, b: number): number {
  return a + b;
}

// file: app.ts
import { add } from "./math";
console.log(add(2, 3));
📜 Declaration Files
declare module "some-library" {
  export function doSomething(): void;
}
🧠 Advanced Types
type UnionType = "success" | "error";
type Intersection = { id: number } & { name: string };

type Conditional<T> = T extends string ? "text" : "other";
type Example = Conditional<"hello">; // "text"
🚀 Tips
  • Enable strict mode in tsconfig.json for safer code.
  • Use never for values that should never occur.
  • Use unknown instead of any for safer type checks.
SQL
Vercel