Web Development
TypeScript
Subjective
Oct 04, 2025
Explain the difference between structural and nominal typing systems.
Detailed Explanation
Structural typing compares type structure, while nominal typing compares type names. TypeScript uses structural typing.
Structural Typing (TypeScript):
interface Point2D {
x: number;
y: number;
}
interface Vector2D {
x: number;
y: number;
}
// These are compatible due to same structure
let point: Point2D = { x: 1, y: 2 };
let vector: Vector2D = point; // OK in TypeScript
Nominal Typing (Languages like Java/C#):
// In Java, these would be different types
class Point2D {
int x, y;
}
class Vector2D {
int x, y;
}
// Point2D point = new Vector2D(); // Error in Java
TypeScript Duck Typing:
function drawPoint(point: { x: number; y: number }) {
console.log('Point at (' + point.x + ', ' + point.y + ')');
}
// Any object with x and y properties works
drawPoint({ x: 1, y: 2 }); // OK
drawPoint({ x: 1, y: 2, z: 3 }); // OK (extra properties allowed)
Implications:
• Structural typing enables duck typing
• More flexible but can allow unintended compatibility
• Nominal typing is more strict but verbose
• TypeScript approach enables JavaScript interoperability.
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts