मुख्य कंटेंट तक स्किप करें
Trushi Jasani
EditReport

Interfaces & Advanced Types

Interfaces and Type Aliases define structured contracts for complex data structures and objects.

1. Interfaces vs Type Aliases

  • Interfaces: Primarily designed to shape object declarations; support structural merging (extending properties with duplicate names).
  • Type Aliases: Can map primitive combinations, structural properties, unions, and tuples. Cannot be open-merged.
// Object modeling via Interface contracts
interface OperatingSystem {
kernelArchitecture: string;
distributionVersion: number;
}

// Structural mapping using Type Aliases
type ScalarMetric = number | string;

2. Structural Extension and Merging

interface ClientProfile {
accountEmail: string;
}

// Extending structural definitions via inheritance contracts
interface CorporateClient extends ClientProfile {
taxRegistrationId: string;
}

3. Union and Intersection Types

Union (|): Restricts a variable to holding any one of several specified type definitions.

Intersection (&): Combines multiple structural types into a single target type contract.

// Union implementation limits options explicitly
let statusIndicator: "Initializing" | "Online" | "Faulted";
statusIndicator = "Online"; // Valid

// Intersection structure joining traits
interface AuditLog { timestamp: Date }
interface SystemEvent { eventId: string }

type FullEventReport = AuditLog & SystemEvent; // Must contain timestamp and eventId fields
Finished reading? Mark this topic as complete.