What is Structural Typing in TypeScript?
Learn what structural typing means in TypeScript, how shape-based compatibility and duck typing work, and how it differs from nominal typing, with examples.
Expected Interview Answer
Structural typing means TypeScript determines type compatibility by an object's shape — its properties and their types — rather than by its declared name or class. If two types have the same members, they are considered compatible.
This is often called duck typing: if it walks and quacks like a duck, it is treated as a duck. A value is assignable to a type when it has at least all the required members of that type, regardless of whether it was declared to implement it. This contrasts with nominal typing, used by languages like Java and C#, where compatibility depends on the explicit type name or inheritance relationship. Structural typing makes TypeScript flexible with plain objects and interfaces, though it can allow unintended matches.
- Enables flexible, duck-typed object compatibility
- Works naturally with plain JavaScript objects
- Reduces boilerplate by not requiring explicit implements declarations
- Makes interfaces and type aliases interchangeable when shapes match
- Simplifies mocking and testing
AI Mentor Explanation
In structural typing, a player qualifies for a role by what they can actually do, not by the label on their contract. Anyone who can keep wicket and bat well counts as a wicketkeeper-batter, even if never officially titled one. TypeScript judges types the same way: if an object has the required skills — the right properties — it fits the role, regardless of the name it was given.
Step-by-Step Explanation
Step 1
Define a target type
Declare an interface or type describing the required properties, e.g. interface Point { x: number; y: number }.
Step 2
Create an object independently
Build a plain object with at least those members, without declaring it implements the interface.
Step 3
Assign by shape
TypeScript allows the assignment because the object structurally matches — it has x and y of the right types.
Step 4
Understand excess properties
Extra members are allowed via a variable, though object literals trigger excess property checks on direct assignment.
Step 5
Contrast with nominal typing
Note that in Java or C#, the object would need to explicitly implement the type; TypeScript does not require this.
What Interviewer Expects
- Defining structural typing as shape-based compatibility
- Contrasting it with nominal typing in languages like Java
- Relating it to duck typing
- Explaining excess property checks on object literals
- Giving a concrete assignment example
Common Mistakes
- Confusing structural typing with nominal typing
- Thinking a class must explicitly implement an interface to be compatible
- Not knowing about excess property checks on object literals
- Assuming type names affect compatibility
- Believing two identically shaped types are ever treated as incompatible
Best Answer (HR Friendly)
“Structural typing means TypeScript decides if two things are compatible by looking at their shape — what properties they have — instead of their name. If an object has all the pieces a type needs, it is accepted, which makes the language flexible with everyday JavaScript objects.”
Code Example
interface Point {
x: number
y: number
}
function logPoint(p: Point) {
console.log(`${p.x}, ${p.y}`)
}
// Never declared as Point, but structurally matches:
const coord = { x: 10, y: 20, label: 'A' }
logPoint(coord) // OK — has x and y
// Object literal triggers excess property check:
logPoint({ x: 1, y: 2, label: 'B' })
// Error: 'label' does not exist in type 'Point'Follow-up Questions
- How does structural typing differ from nominal typing?
- What are excess property checks and when do they apply?
- How can you simulate nominal typing in TypeScript with branded types?
- Does a class need to implement an interface to be assignable to it?
- How does structural typing affect function parameter compatibility?
MCQ Practice
1. TypeScript's type compatibility is based primarily on?
TypeScript uses structural typing, so compatibility depends on the members an object has, not its name.
2. Which term is a synonym often used for structural typing?
Structural typing is commonly called duck typing: if it has the right shape, it is treated as that type.
3. When does an excess property error occur?
Excess property checks fire on fresh object literals assigned directly, not on values assigned via a variable.
Flash Cards
What is structural typing? — Type compatibility based on an object's shape (its members), not its declared name or class.
What is the opposite of structural typing? — Nominal typing, used by Java and C#, where the explicit type name determines compatibility.
What is another name for structural typing? — Duck typing — if it has the right properties, it is treated as that type.
When do excess property checks apply? — When a fresh object literal is assigned directly to a typed target, not when assigned via a variable.