Interfaces & Types
Define interface pattern
interface User {
id: number;
name: string;
}
Optional properties
interface Product {
name: string;
price?: number; // Optional
}
Readonly properties
interface Config {
readonly apiKey: string;
}
Index signature (dictionary pattern)
interface StringMap {
[key: string]: string;
}
Extend interface
interface Admin extends User {
permissions: string[];
}
Define type alias
type ID = string | number;
Object type alias
type Point = { x: number; y: number };
Union type
type Status = "pending" | "approved";
Intersection type
type Staff = Person & Employee;
Function type alias
type Callback = (data: string) => void;
Partial utility type (all optional)
type PartialUser = Partial<User>;
Pick utility type (select keys)
type UserLogin = Pick<User, "email" | "password">;