TypeScript Interface Merging: Unified Data Structures
In many real-world applications, data often originates from disparate sources, each defining its own structure. When these structures represent related entities, it becomes crucial to consolidate them into a single, cohesive representation. This challenge focuses on a common scenario in TypeScript where you need to combine multiple interface definitions into a single, unified interface. This is particularly useful when dealing with modular codebases or when integrating with external APIs that might provide data in fragmented formats.
Problem Description
Your task is to create a TypeScript interface that effectively merges properties from multiple existing interfaces. This merged interface should encompass all properties from all contributing interfaces. If there are any property name collisions between the contributing interfaces, the merged interface should prioritize the type from a specific interface (as defined by the problem).
Key Requirements:
- Define three distinct TypeScript interfaces:
BaseInfo: With properties likeid(number) andname(string).ContactDetails: With properties likeemail(string) andphone(string).AddressInfo: With properties likestreet(string),city(string), andzipCode(string).
- Create a new interface,
UnifiedProfile, that mergesBaseInfo,ContactDetails, andAddressInfo. - Handle Property Collisions: For the purpose of this challenge, assume there are no direct name collisions between the properties of
BaseInfo,ContactDetails, andAddressInfo. If there were, a common approach is to use an intersection type (&), which would result in a union type for properties with different types, or require manual resolution. For this exercise, focus on simple merging. - Demonstrate usage: Provide a clear example of how
UnifiedProfilecan be used to define an object.
Examples
Example 1:
// Assume these interfaces are defined
interface BaseInfo {
id: number;
name: string;
}
interface ContactDetails {
email: string;
phone: string;
}
interface AddressInfo {
street: string;
city: string;
zipCode: string;
}
// The goal is to create UnifiedProfile by merging these.
// Example usage:
const userProfile: UnifiedProfile = {
id: 123,
name: "Alice Wonderland",
email: "alice@example.com",
phone: "123-456-7890",
street: "123 Main St",
city: "Anytown",
zipCode: "12345"
};
Output (Type Definition):
The UnifiedProfile interface should be defined such that it includes all properties from BaseInfo, ContactDetails, and AddressInfo. The actual output is the type definition itself, not a runtime value.
Explanation:
The UnifiedProfile interface successfully combines the properties of BaseInfo (id, name), ContactDetails (email, phone), and AddressInfo (street, city, zipCode) into a single, comprehensive type. The example usage demonstrates that an object conforming to UnifiedProfile must provide values for all these properties.
Constraints
- The names of the interfaces to be merged are fixed:
BaseInfo,ContactDetails,AddressInfo. - The properties and their types within these initial interfaces are as described in the problem description.
- Focus on static interface merging using TypeScript's built-in features.
- No runtime code is required for the merging itself; the solution is a type definition.
Notes
Consider how TypeScript handles the merging of declarations with the same name. This is a core concept in TypeScript for extending interfaces. Think about how you can combine multiple types to create a new one that contains all the members of the constituent types. The use of intersection types (&) is a powerful tool for this.