Hello, object builder! In Module Two, you learned about basic types like string, number, and boolean. Now it is time to learn how to describe objects β things that have many properties.
In TypeScript, we use interfaces and type aliases to describe the shape of objects. They help you define what properties an object should have and what types those properties should be.
In this module, you will learn how to use interfaces and type aliases to create clear, organised, and safe code.
Let's start describing objects with TypeScript!
By the end of this module, you will be able to:
In Lagos, a developer named Kemi was building an app for a store. She needed to store information about customers β name, age, email, and whether they were active.
She tried using basic types, but it was messy. She had to remember that a customer had a name (string), age (number), email (string), and isActive (boolean).
Her mentor said, "Kemi, you need an interface β it's like a blueprint that describes what a customer should look like."
Kemi created an interface called Customer. It defined that a customer has a name (string), age (number), email (string), and isActive (boolean).
Now, whenever she created a customer, TypeScript checked that it had all the right properties. Kemi said, "Interfaces make my code so much cleaner and safer!"
Definition: An object is a collection of properties β like a person with a name, age, and email.
Why is it important? Objects are used to represent real-world things in code.
Simple explanation: An object is like a box that holds many pieces of information.
Real-life example: A customer has a name, age, and email.
School example: A student has a name, grade, and class.
Home example: A family member has a name, age, and role.
Nigerian example: A product has a name, price, and description.
let customer = {
name: "Ada",
age: 25,
email: "ada@example.com"
};
Mini summary: Objects hold collections of properties.
Definition: An interface is a way to describe the shape of an object β what properties it should have and what types they are.
Why is it important? It helps you define a blueprint for objects.
Simple explanation: An interface is like a template for an object.
Real-life example: A blueprint for a house.
School example: A template for a student record.
Home example: A form that needs to be filled out.
Nigerian example: A template for a product listing.
interface Customer {
name: string;
age: number;
email: string;
}
Mini summary: An interface describes the shape of an object.
Definition: You use an interface by applying it to an object β the object must have the properties defined in the interface.
Why is it important? It ensures that objects have the correct shape.
Simple explanation: You tell TypeScript that an object should follow the interface.
Real-life example: let customer: Customer = { name: "Ada", age: 25, email: "ada@example.com" };
School example: let student: Student = { name: "Chidi", grade: 90 };
Home example: let person: Person = { name: "Bola", age: 30 };
Nigerian example: let product: Product = { name: "Rice", price: 500 };
let customer: Customer = {
name: "Ada",
age: 25,
email: "ada@example.com"
};
Mini summary: You apply an interface to an object.
Definition: Optional properties are properties that do not have to be present in the object.
Why is it important? Sometimes an object may not have all properties.
Simple explanation: You add a ? after the property name to make it optional.
Real-life example: A customer may or may not have a phone number.
School example: A student may or may not have an email address.
Home example: A family member may or may not have a middle name.
Nigerian example: A product may or may not have a description.
interface Customer {
name: string;
age: number;
email?: string; // optional
}
Mini summary: Optional properties are not required.
Definition: Readonly properties cannot be changed after they are set.
Why is it important? It protects data that should not be modified.
Simple explanation: You add readonly before the property name.
Real-life example: A customer's ID should not be changed.
School example: A student's enrolment number should not be changed.
Home example: A family member's date of birth should not be changed.
Nigerian example: A product's SKU should not be changed.
interface Customer {
readonly id: number;
name: string;
age: number;
}
Mini summary: Readonly properties cannot be changed.
Definition: A type alias is a way to give a name to a type β it can be used for any type, not just objects.
Why is it important? It makes complex types easier to use.
Simple explanation: A type alias is like a nickname for a type.
Real-life example: type Name = string;
School example: type Grade = number;
Home example: type Age = number;
Nigerian example: type Price = number;
type Name = string;
type Age = number;
type Person = {
name: Name;
age: Age;
};
Mini summary: A type alias gives a name to a type.
Definition: Interfaces and type aliases are similar but have some differences.
Why is it important? You need to choose the right tool for the job.
Simple explanation: Interfaces are best for objects; type aliases can be used for anything.
Real-life example: Use interface for a customer object.
School example: Use interface for a student record.
Home example: Use type alias for a simple type like number.
Nigerian example: Use interface for a product.
interface Customer {
name: string;
age: number;
}
type ID = string | number;
Mini summary: Interfaces are for objects; type aliases are for any type.
Definition: You can extend an interface to create a new interface that includes all properties of the original.
Why is it important? It promotes reusability.
Simple explanation: Like inheriting from a parent.
Real-life example: A VIPCustomer extends Customer with a discount property.
School example: A GraduateStudent extends Student with a degree property.
Home example: A FamilyMember extends Person with a role property.
Nigerian example: A PremiumProduct extends Product with a warranty property.
interface Customer {
name: string;
age: number;
}
interface VIPCustomer extends Customer {
discount: number;
}
Mini summary: Extending interfaces reuses properties.
Definition: Intersection types combine multiple types into one.
Why is it important? It allows you to combine types.
Simple explanation: Like mixing two things together.
Real-life example: A Customer and a VIP combined.
School example: A Student and an Athlete combined.
Home example: A Person and a Worker combined.
Nigerian example: A Product and a DiscountedProduct combined.
type Customer = { name: string; age: number };
type VIP = { discount: number };
type VIPCustomer = Customer & VIP;
Mini summary: Intersection types combine types.
Definition: Union types allow a value to be one of several types.
Why is it important? It provides flexibility.
Simple explanation: Like saying "this can be a string or a number."
Real-life example: A customer's ID can be a string or a number.
School example: A student's grade can be a number or a letter.
Home example: A family member's age can be a number or "unknown."
Nigerian example: A product's price can be a number or "free."
type ID = string | number; let id: ID = "123"; id = 123; // also valid
Mini summary: Union types allow multiple types.
Definition: You can use interfaces to describe the shape of parameters and return values.
Why is it important? It ensures functions are used correctly.
Simple explanation: You tell TypeScript what kind of object a function expects.
Real-life example: function greet(customer: Customer): string { ... }
School example: function getGrade(student: Student): number { ... }
Home example: function getAge(person: Person): number { ... }
Nigerian example: function getPrice(product: Product): number { ... }
interface Customer {
name: string;
age: number;
}
function greet(customer: Customer): string {
return `Hello ${customer.name}`;
}
Mini summary: Interfaces can describe function parameters.
Definition: Type assertions tell TypeScript that you know more about the type than it does.
Why is it important? It helps when you are sure about the type.
Simple explanation: Like saying "trust me, I know what type this is."
Real-life example: let value =
School example: let result =
Home example: let item =
Nigerian example: let product =
let someValue: any = { name: "Ada", age: 25 };
let customer = someValue;
Mini summary: Type assertions tell TypeScript what type to expect.
Definition: Common mistakes are errors that beginners often make with interfaces.
Why is it important? Avoiding them saves time and frustration.
Simple explanation: Don't forget required properties.
Real-life example: Missing a required property in an object.
School example: Forgetting to include a student's name.
Home example: Forgetting a family member's age.
Nigerian example: Forgetting a product's price.
interface Customer {
name: string;
age: number;
}
// Error: missing age
let customer: Customer = { name: "Ada" };
Mini summary: Always include required properties.
Definition: Best practices are the recommended ways to use interfaces.
Why is it important? They make your code clean and safe.
Simple explanation: Use interfaces to describe objects, and use type aliases for other types.
Real-life example: Use interface for customer, product, etc.
School example: Use interface for student, teacher, etc.
Home example: Use interface for person, family, etc.
Nigerian example: Use interface for product, order, etc.
interface Customer {
name: string;
age: number;
email?: string;
}
Mini summary: Use interfaces for objects; use type aliases for other types.
Definition: You are now an interface master β you can describe objects with interfaces and type aliases.
Why is it important? You can now write clean, organised, and safe code.
Simple explanation: You have mastered interfaces and type aliases.
Real-life example: You can now define complex objects in your code.
School example: You can use interfaces in school projects.
Home example: You can practice at home.
Nigerian example: You can build apps with TypeScript in Nigeria.
Interface Master Checklist: - Defines interfaces - Uses optional and readonly properties - Uses type aliases - Extends interfaces - Uses union and intersection types
Mini summary: You are now an interface master!
| Word | Simple Definition |
|---|---|
| Interface | A description of an object's shape. |
| Object | A collection of properties. |
| Property | A piece of data inside an object. |
| Optional | Not required. |
| Readonly | Cannot be changed. |
| Type Alias | A name for a type. |
| Extend | Inherit from another interface. |
| Union | One of several types. |
| Intersection | Combine multiple types. |
| Assertion | Tell TypeScript the type. |
interface Customer {
name: string;
age: number;
email?: string;
}
type ID = string | number;
type Person = {
name: string;
age: number;
};
+-----------------------+-----------------------+ | Interface | Type Alias | +-----------------------+-----------------------+ | For objects | For any type | | Extendable | Not extendable | | Best for objects | Best for simple types | | More readable | More flexible | +-----------------------+-----------------------+
| Feature | Interface | Type Alias |
|---|---|---|
| For objects | Yes | Yes |
| For primitives | No | Yes |
| Extendable | Yes | No |
| Union types | No | Yes |
| Tuple types | No | Yes |
| Type | Description | Example |
|---|---|---|
| Required | Must be present | name: string |
| Optional | May be present | email?: string |
| Readonly | Cannot be changed | readonly id: number |
(Included after each lesson above β review them!)
In this module, you learned about interfaces and type aliases. You discovered how to describe objects using interfaces and how to give names to types using type aliases. You also learned about optional properties, readonly properties, extending interfaces, and union and intersection types. You are now an interface master. In the next module, you will learn about functions in TypeScript. Congratulations on completing Module Three!
Match the term with its definition.
| Term | Definition |
|---|---|
| 1. Interface | A. Describes an object |
| 2. Type Alias | B. Name for a type |
| 3. Optional | C. Not required |
| 4. Readonly | D. Cannot be changed |
| 5. Union | E. One of several types |
Answers: 1-A, 2-B, 3-C, 4-D, 5-E
βInterface Designβ
In groups, design interfaces for a real-world scenario (like a store or a school). Share your designs with the class.
βMy Interfacesβ
Define interfaces for a business scenario (like a store or a school). Use optional and readonly properties. Share your code with the class.
βTypeScript Data Modelβ
Create a complete data model for a business using interfaces and type aliases. Include optional and readonly properties. Present your model to the class.
βWrite TypeScript Codeβ
Write a TypeScript program that uses interfaces and type aliases. Include optional and readonly properties. Compile and run it. Share your code and output.
βInterface Challengeβ
Create a complex data structure using interfaces, type aliases, union types, and intersection types. Share your solution with the class.
(Answers to fill-in-the-blank, true/false, and multiple choice are provided within those sections.)
In Module Four, you will learn about functions. To prepare:
Get ready to learn about functions in TypeScript!
End of Module Three Β· You are now an interface master!