Hone logo
Hone
Problems

Platform Type Hierarchy in TypeScript

This challenge focuses on creating a robust and extensible type system in TypeScript to represent different types of online platforms. You'll define a base Platform type and then derive specialized platform types (e.g., SocialMediaPlatform, ECommercePlatform, GamingPlatform) with specific properties. This exercise reinforces inheritance, type safety, and the power of TypeScript's type system for modeling real-world entities.

Problem Description

You are tasked with creating a TypeScript type hierarchy to represent various online platforms. The base Platform type should have a common name (string) and url (string) property. Then, you need to define three derived types:

  • SocialMediaPlatform: Extends Platform and adds a followersCount (number) property.
  • ECommercePlatform: Extends Platform and adds productCount (number) and averageRating (number) properties.
  • GamingPlatform: Extends Platform and adds gameLibrarySize (number) and subscriptionModel (string - e.g., "Free-to-Play", "Subscription", "Premium").

Finally, create a type PlatformCategory which is a union of the three derived platform types: SocialMediaPlatform | ECommercePlatform | GamingPlatform. Write a function describePlatform(platform: PlatformCategory) that takes a PlatformCategory as input and returns a string describing the platform, including its name, URL, and relevant platform-specific details.

Key Requirements:

  • Proper inheritance using extends.
  • Type safety for all properties.
  • Correct union type definition for PlatformCategory.
  • The describePlatform function should handle all three platform types correctly.

Expected Behavior:

The describePlatform function should produce informative strings based on the platform type passed to it. For example, a SocialMediaPlatform description should include the follower count.

Edge Cases to Consider:

  • Ensure the code compiles without errors.
  • Consider how to handle potential future platform types (the design should be extensible).
  • The describePlatform function should not throw errors when encountering any of the platform types.

Examples

Example 1:

Input: {
  name: "Twitter",
  url: "https://twitter.com",
  followersCount: 123456789
}
Output: "Platform: Twitter, URL: https://twitter.com, Followers: 123456789"
Explanation: This is a SocialMediaPlatform, so the output includes the followersCount.

Example 2:

Input: {
  name: "Amazon",
  url: "https://amazon.com",
  productCount: 1000000,
  averageRating: 4.5
}
Output: "Platform: Amazon, URL: https://amazon.com, Products: 1000000, Average Rating: 4.5"
Explanation: This is an ECommercePlatform, so the output includes productCount and averageRating.

Example 3:

Input: {
  name: "Steam",
  url: "https://store.steampowered.com",
  gameLibrarySize: 10000,
  subscriptionModel: "Premium"
}
Output: "Platform: Steam, URL: https://store.steampowered.com, Games: 10000, Subscription Model: Premium"
Explanation: This is a GamingPlatform, so the output includes gameLibrarySize and subscriptionModel.

Constraints

  • All property names must be strings.
  • followersCount, productCount, and gameLibrarySize must be non-negative numbers.
  • The describePlatform function must execute in under 1 millisecond for any valid input. (This is more of a conceptual constraint - focus on code clarity and correctness first).
  • The code must be valid TypeScript and compile without errors.

Notes

  • Think about how you can make the type hierarchy extensible to accommodate new platform types in the future. Consider using interfaces or abstract classes.
  • The describePlatform function can use template literals for string interpolation.
  • Focus on creating a well-typed and maintainable solution. Avoid using any unless absolutely necessary.
  • Consider using discriminated unions to simplify the logic within the describePlatform function.
Loading editor...
typescript