🔍 Overview
Chapter 7 introduces the concept of subtyping, which allows one type to be treated as another. It explores how subtyping supports flexibility and reuse in type systems, especially in object-oriented and functional programming.
💡 Key Concepts
- Subtyping Basics: Explains how a subtype can be used wherever its supertype is expected.
- Structural Subtyping: Describes how TypeScript uses shape-based compatibility rather than nominal inheritance.
- Function Subtyping: Covers how parameter and return types affect function compatibility.
- Covariance and Contravariance: Introduces variance rules for function parameters and return types.
- Type Compatibility: Shows how TypeScript determines whether one type can be assigned to another.
- Pitfalls and Gotchas: Highlights common mistakes and edge cases in subtyping relationships.
🧭 Takeaway
Subtyping enables flexible and reusable code, but it requires careful attention to compatibility rules. Understanding structural subtyping and variance helps developers write safer and more predictable TypeScript programs.