Object-Oriented TypeScript πŸ›οΈ

Intro

Intro to Object Oriented Typescript
πŸ‘¨β€πŸ’Ό Welcome to the Object-Oriented Programming workshop! This workshop focuses on encapsulation and polymorphism, not just syntax.
Object-oriented programming (OOP) is a way to structure code around objects that combine data and behavior. TypeScript makes OOP powerful with its type system.
We'll cover:
  1. Classes - Fields, methods, constructors, private fields, and defaults
  2. Interfaces and Classes - Implementing contracts and programming to abstractions
  3. Inheritance and Polymorphism - Extending classes, overriding methods, and substitutability
  4. Composition vs Inheritance - Dependency injection and practical decision-making

OOP vs Functional Programming

TypeScript supports multiple paradigms. This workshop teaches OOP because it's valuable knowledgeβ€”you'll encounter it in many codebases and libraries. But you should know the alternative:
OOP organizes around objects - data and behavior bundled together:
class ShoppingCart {
	#items: Array<Product> = []
	addItem(product: Product) {
		this.#items.push(product)
	}
	getTotal() {
		return this.#items.reduce((sum, p) => sum + p.price, 0)
	}
}
FP organizes around functions - data and transformations separate:
type Cart = ReadonlyArray<Product>
const addItem = (cart: Cart, product: Product): Cart => [...cart, product]
const getTotal = (cart: Cart): number =>
	cart.reduce((sum, p) => sum + p.price, 0)
Both are valid. OOP excels at encapsulation and polymorphism. FP excels at data transformation and immutability. Choose based on your problem domain.
Pragmatism over purity! JavaScript isn't Haskellβ€”no JS program is 100% purely functional. If a class solves your problem elegantly, use it. The goal is to have both tools in your toolkit and choose wisely. Modern React codebases often lean functional, but many successful projects use classes too.
By the end of this workshop, you'll be able to:
  • Create classes with proper encapsulation using #private fields
  • Use interfaces to define contracts
  • Apply inheritance appropriately
  • Understand when to prefer composition
  • Write polymorphic code that's flexible and testable
  • Make informed decisions about when OOP or FP is the better fit
Let's build some objects! 🧱
🎡 Check out the workshop theme song! 🎢
Loading "Object Oriented TypeScript Theme Song"