Master Object-Oriented Programming with classes, enums, and powerful utility types in TypeScript.
Learn classes with access modifiers, enums, and TypeScript's built-in utility types.
Person class with properties name and age. Use the constructor shorthand:class Person {
constructor(
public name: string,
public age: number
) {}
greet(): string {
return "Hello, I'm " + this.name;
}
}public - accessible everywhere (default)private - only accessible within the classprotected - accessible within class and subclassesBankAccount class with a private balance and public methods to deposit/withdraw:class BankAccount {
private balance: number = 0;
deposit(amount: number): void {
this.balance += amount;
}
getBalance(): number {
return this.balance;
}
}Employee class that extends Person. Add an employeeId property and override the greet method using super:class Employee extends Person {
constructor(name: string, age: number, public employeeId: string) {
super(name, age); // Call parent constructor
}
greet(): string {
return super.greet() + " (Employee #" + this.employeeId + ")";
}
}abstract class Shape {
abstract getArea(): number; // Must be implemented by subclasses
describe(): string {
return "Area: " + this.getArea();
}
}Circle and Rectangle classes that extend Shape and implement getArea().Person: Hello, I'm Alice
Account balance: $150
Employee: Hello, I'm Bob (Employee #E001)
Circle area: 78.54
Rectangle area: 24
Shape description: Area: 78.54
constructor(public name: string) {}private balance: number = 0;class Employee extends Person { }super(name, age) in constructor, super.method() for methodsabstract class Shape { abstract getArea(): number; }Review feedback below
enum Direction {
North, // 0
South, // 1
East, // 2
West // 3
}
let heading: Direction = Direction.North;
console.log(heading); // Output: 0enum HttpStatus {
OK = 200,
BadRequest = 400,
Unauthorized = 401,
NotFound = 404,
InternalError = 500
}enum Color {
Red = "RED",
Green = "GREEN",
Blue = "BLUE"
}
let favorite: Color = Color.Blue;
console.log(favorite); // Output: "BLUE"getStatusMessage that takes an HttpStatus and returns an appropriate message using a switch statement:function getStatusMessage(status: HttpStatus): string {
switch (status) {
case HttpStatus.OK:
return "Success!";
case HttpStatus.NotFound:
return "Resource not found";
// ... etc
}
}console.log(Direction[0]); // "North"
console.log(HttpStatus[404]); // "NotFound"Direction North: 0
Direction name from 2: East
HTTP 200: Success!
HTTP 404: Resource not found
Color: BLUE
User role: ADMIN
Is admin: true
enum Direction { North, South, East, West }enum Status { OK = 200, NotFound = 404 }enum Color { Red = "RED" }let dir: Direction = Direction.North;Direction[0] returns "North"Review feedback below
interface User {
id: number;
name: string;
email: string;
}
// All properties become optional
type PartialUser = Partial<User>;
// Same as: { id?: number; name?: string; email?: string; }
function updateUser(user: User, updates: Partial<User>): User {
return { ...user, ...updates };
}interface Config {
host?: string;
port?: number;
}
type RequiredConfig = Required<Config>;
// Now host and port are both requiredtype ReadonlyUser = Readonly<User>;
const user: ReadonlyUser = { id: 1, name: "Alice", email: "a@b.com" };
// user.name = "Bob"; // ERROR: Cannot assign to 'name'type UserPreview = Pick<User, "id" | "name">;
// Same as: { id: number; name: string; }type UserWithoutEmail = Omit<User, "email">;
// Same as: { id: number; name: string; }type UserRoles = Record<string, boolean>;
const roles: UserRoles = {
admin: true,
editor: false
};Original: { id: 1, name: "Alice", email: "alice@test.com" }
Updated: { id: 1, name: "Alice Updated", email: "alice@test.com" }
User preview (Pick): { id: 1, name: "Alice" }
Without email (Omit): { id: 1, name: "Alice" }
Roles record: { admin: true, editor: false, viewer: true }
type PartialUser = Partial<User>; makes all props optionaltype RequiredConfig = Required<Config>;type ImmutableUser = Readonly<User>;type Preview = Pick<User, "id" | "name">;type NoEmail = Omit<User, "email">;type Dict = Record<string, number>;Review feedback below