TypeScript Master (50 Qs)•Part 1: Basic & Foundational TypeScript (Junior / Entry-Level)
What is Structural Typing (Duck Typing)?
junior LevelCode ExampleTypeScript
In Simple Words: TypeScript uses structural typing, not nominal typing. Two types are considered identical if they have the same structure (shape), regardless of their declared names:
typescript
interface Point2D { x: number; y: number; }
class Coordinate { x = 0; y = 0; }
let p: Point2D = new Coordinate(); // Perfectly valid! Structure matches.
If an object looks like a duck and quacks like a duck, TypeScript treats it as a duck.