What structural typing means
📖 Scenario: Imagine you are building a simple system where you want to check if different objects can be used in the same way based on their shape, not their exact type name.
🎯 Goal: You will create objects and functions to see how TypeScript uses structural typing to allow objects with the same shape to be used interchangeably.
📋 What You'll Learn
Create an interface called
Person with properties name (string) and age (number).Create an object called
user that matches the Person interface exactly.Create another object called
employee that has the same properties as Person plus an extra property salary (number).Write a function called
greet that takes a parameter of type Person and returns a greeting string using the name property.Call
greet with both user and employee and print the results.💡 Why This Matters
🌍 Real World
Structural typing helps when working with data from different sources that share common properties, like user profiles or API responses.
💼 Career
Understanding structural typing is key for TypeScript developers to write flexible and type-safe code that integrates well with various libraries and data shapes.
Progress0 / 4 steps