Structural Type Checking Mechanics
You'll learn: How TypeScript compares types by their shape rather than their names during type inference and checking.
The Fundamental Mechanism
When TypeScript checks if one type is compatible with another during inference, it doesn't care what the types are called. It only cares about what properties and methods they have. This is called structural typing (or "duck typing" at the type level).
Think of it like a security checkpoint that only checks what's in your bag, not what brand of bag you're carrying. If you have the required items (properties), you pass—regardless of the bag's label (type name).
How Structural Checking Works
When TypeScript encounters a type assignment or function call, it walks through the structure:
- Does the source type have all the properties the target type requires?
- Are those properties' types compatible (recursively applying the same check)?
- If yes to both, the types are compatible—assignment succeeds
The names of the types involved are completely irrelevant. Two interfaces could be defined in different files, never import each other, and still be considered identical if their structure matches.
Shape Matching During Inference
When TypeScript infers types from values, it looks at the literal structure you've provided. An object literal { name: string, age: number } matches any type expecting those two properties—whether that type is called Person, User, or has no name at all.
This structural approach is what makes TypeScript's inference so flexible during generic type argument resolution, union narrowing, and return type deduction. The compiler doesn't need explicit type conversions—it just checks shapes.
Key Takeaway: TypeScript compares types by matching their internal structure (properties and their types), not by checking whether type names match—enabling flexible, inference-friendly type checking that focuses on capabilities rather than labels.