 Extending and Combining Interfaces
Extending and Combining Interfaces
When building applications in TypeScript, you often need to create complex types that share common properties or behaviors. Rather than duplicating code, you can use interface extension and combination to assemble flexible and reusable types. You achieve this by extending interfaces with the extends keyword or combining multiple interfaces using intersection types with the & operator. These features let you model real-world relationships and requirements more effectively.
12345678910111213141516interface Person { name: string; age: number; } interface Employee extends Person { employeeId: number; department: string; } const developer: Employee = { name: "Alice", age: 30, employeeId: 101, department: "Engineering" };
By using interface extension, you build upon existing interfaces to create more specialized types. In the example above, the Employee interface extends Person, inheriting its properties and adding new ones. This approach promotes code reuse because you define shared properties once and extend them as needed. It also increases type safety, ensuring that any Employee will always have all properties defined in Person as well as its own.
1234567891011121314interface Contactable { phone: string; } interface Emailable { email: string; } type ContactInfo = Contactable & Emailable; const customer: ContactInfo = { phone: "555-1234", email: "customer@example.com" };
Extending and combining interfaces with intersection types is useful when you want to describe objects that must satisfy multiple contracts at once. This is especially practical when modeling entities with multiple roles or capabilities. For instance, if you have different interfaces for contact methods, you can combine them to represent a user who must have both a phone number and an email address.
Extending and combining interfaces is common when modeling user roles, permissions, or entities with shared features. For example, an Admin interface might extend a User interface and add properties specific to administrators, while a Manager might combine both Employee and Supervisor interfaces to reflect their responsibilities.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 9.09 Extending and Combining Interfaces
Extending and Combining Interfaces
Deslize para mostrar o menu
When building applications in TypeScript, you often need to create complex types that share common properties or behaviors. Rather than duplicating code, you can use interface extension and combination to assemble flexible and reusable types. You achieve this by extending interfaces with the extends keyword or combining multiple interfaces using intersection types with the & operator. These features let you model real-world relationships and requirements more effectively.
12345678910111213141516interface Person { name: string; age: number; } interface Employee extends Person { employeeId: number; department: string; } const developer: Employee = { name: "Alice", age: 30, employeeId: 101, department: "Engineering" };
By using interface extension, you build upon existing interfaces to create more specialized types. In the example above, the Employee interface extends Person, inheriting its properties and adding new ones. This approach promotes code reuse because you define shared properties once and extend them as needed. It also increases type safety, ensuring that any Employee will always have all properties defined in Person as well as its own.
1234567891011121314interface Contactable { phone: string; } interface Emailable { email: string; } type ContactInfo = Contactable & Emailable; const customer: ContactInfo = { phone: "555-1234", email: "customer@example.com" };
Extending and combining interfaces with intersection types is useful when you want to describe objects that must satisfy multiple contracts at once. This is especially practical when modeling entities with multiple roles or capabilities. For instance, if you have different interfaces for contact methods, you can combine them to represent a user who must have both a phone number and an email address.
Extending and combining interfaces is common when modeling user roles, permissions, or entities with shared features. For example, an Admin interface might extend a User interface and add properties specific to administrators, while a Manager might combine both Employee and Supervisor interfaces to reflect their responsibilities.
Obrigado pelo seu feedback!