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:
- Classes - Fields, methods, constructors, private fields, and defaults
- Interfaces and Classes - Implementing contracts and programming to abstractions
- Inheritance and Polymorphism - Extending classes, overriding methods, and substitutability
- 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
#privatefields - 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"