TypeScript Platform Type System
You are tasked with building a flexible and type-safe system in TypeScript to represent different types of platforms. This system should allow for defining common platform properties and then extending these with platform-specific attributes. This is a common pattern in application development where you might have services that can run on various infrastructure types (e.g., cloud servers, on-premises hardware, edge devices), each with its own unique characteristics.
Problem Description
The goal is to create a TypeScript type structure that can model various platforms, each with a set of common properties and potentially unique, type-specific properties. You need to define a base type for all platforms and then create derived types for specific platform categories, such as "CloudPlatform" and "OnPremisePlatform". The system should be extensible, allowing for easy addition of new platform types in the future.
Key Requirements:
-
Base Platform Type: Define a base
Platformtype that includes common properties applicable to all platforms. These common properties should include:id: A unique identifier for the platform (e.g., a string UUID).name: A human-readable name for the platform.status: The current operational status of the platform (e.g., 'active', 'inactive', 'maintenance').
-
Derived Platform Types: Create at least two specific platform types that inherit from the
Platformtype and add their own unique properties:CloudPlatform: Should inherit fromPlatformand include an additional property:cloudProvider: The name of the cloud provider (e.g., 'AWS', 'Azure', 'GCP').
OnPremisePlatform: Should inherit fromPlatformand include additional properties:datacenterLocation: The physical location of the on-premises datacenter.serverCount: The number of servers in the on-premises environment.
-
Type Safety: Ensure that when you work with instances of these platform types, TypeScript enforces type correctness. For example, you should not be able to assign a
cloudProviderto anOnPremisePlatforminstance. -
Extensibility: Design the system so that it's straightforward to add new platform types (e.g.,
EdgePlatform) in the future without significantly altering existing code.
Expected Behavior:
The code should define interfaces or types for Platform, CloudPlatform, and OnPremisePlatform that correctly model the described properties and inheritance. Functions that operate on these platforms should be able to accept specific platform types or a union of platform types, with type guards to differentiate between them if necessary.
Edge Cases to Consider:
- How would you handle a scenario where a platform might have properties from multiple derived types (though this challenge focuses on single inheritance-like structures through composition/extension)?
- Consider the case where a platform might be neither a
CloudPlatformnor anOnPremisePlatformbut still needs to be represented.
Examples
Example 1: Defining and Instantiating Platforms
// Assume the types Platform, CloudPlatform, and OnPremisePlatform are defined as per requirements.
const awsPlatform: CloudPlatform = {
id: 'uuid-aws-123',
name: 'AWS US East 1',
status: 'active',
cloudProvider: 'AWS',
};
const localDatacenter: OnPremisePlatform = {
id: 'uuid-local-456',
name: 'Main Data Center',
status: 'maintenance',
datacenterLocation: 'New York, USA',
serverCount: 150,
};
// This should be a TypeScript error:
// const invalidPlatform: CloudPlatform = {
// id: 'uuid-invalid-789',
// name: 'Invalid Platform',
// status: 'inactive',
// datacenterLocation: 'Some City', // Error: Property 'datacenterLocation' does not exist on type 'CloudPlatform'.
// };
Explanation: This example demonstrates how to create concrete instances of CloudPlatform and OnPremisePlatform, ensuring that all required properties are present for their respective types. It also highlights a scenario that would result in a TypeScript compilation error, enforcing type safety.
Example 2: Function Accepting a Union of Platforms
// Assume the types Platform, CloudPlatform, and OnPremisePlatform are defined.
// Assume awsPlatform and localDatacenter from Example 1 are available.
type AnyPlatform = CloudPlatform | OnPremisePlatform;
function describePlatform(platform: AnyPlatform): string {
let description = `Platform: ${platform.name} (ID: ${platform.id}, Status: ${platform.status})`;
if ('cloudProvider' in platform) {
description += ` - Cloud Provider: ${platform.cloudProvider}`;
} else if ('datacenterLocation' in platform) {
description += ` - Datacenter: ${platform.datacenterLocation}, Servers: ${platform.serverCount}`;
}
return description;
}
console.log(describePlatform(awsPlatform));
console.log(describePlatform(localDatacenter));
Expected Output:
Platform: AWS US East 1 (ID: uuid-aws-123, Status: active) - Cloud Provider: AWS
Platform: Main Data Center (ID: uuid-local-456, Status: maintenance) - Datacenter: New York, USA, Servers: 150
Explanation: This example shows a function describePlatform that can accept any platform type. It uses a type guard ('propertyName' in object) to dynamically check which specific platform type it's dealing with and access its unique properties.
Constraints
- Your solution should use TypeScript interfaces or type aliases.
- The code should be compilable and type-checked by the TypeScript compiler.
- No runtime JavaScript libraries should be required for the type definitions themselves.
- Focus on the type-level implementation rather than complex runtime logic.
Notes
- Consider how
extendskeyword in TypeScript interfaces can be used for inheritance. - Think about using union types to represent a collection of different platform types.
- Type guards are crucial for safely narrowing down types within functions that accept union types.
- This exercise is about modeling data structures and leveraging TypeScript's type system for safety and clarity.