Hone logo
Hone
Problems

Designing and Implementing Protocol Types in TypeScript

Protocol types in TypeScript allow you to define a contract for how objects should behave, focusing on the methods and properties they expose rather than their internal structure. This is particularly useful for defining interfaces between different modules or services, promoting loose coupling and enabling more robust and maintainable code. This challenge will guide you through creating and utilizing protocol types to enforce a specific behavior across different object implementations.

Problem Description

You are tasked with designing and implementing protocol types in TypeScript to define a Loggable protocol. This protocol dictates that any object adhering to it must have a log method that accepts a string message and prints it to the console. You need to create a base protocol type, then implement two different classes – ConsoleLogger and FileLogger – that conform to this protocol. ConsoleLogger should log messages to the console, while FileLogger should simulate logging to a file (you can simply print to the console indicating a file write).

Key Requirements:

  • Define a Loggable protocol type using TypeScript's protocol type feature.
  • Create two classes, ConsoleLogger and FileLogger, that implement the Loggable protocol.
  • Ensure that both classes have a log method that accepts a string message.
  • ConsoleLogger's log method should print the message to the console using console.log().
  • FileLogger's log method should print a message to the console indicating that the message has been written to a file (e.g., "Writing to file: [message]").
  • Demonstrate the usage of both logger classes by creating instances and calling their log methods.

Expected Behavior:

When you run the code, you should see the following output:

ConsoleLogger: Hello, world!
FileLogger: Writing to file: Hello, world!
ConsoleLogger: This is a test.
FileLogger: Writing to file: This is a test.

Edge Cases to Consider:

  • What happens if a class attempts to implement Loggable but doesn't provide a log method? TypeScript's type system should catch this error during compilation.
  • Consider how you might extend the Loggable protocol in the future to include additional logging methods (e.g., logError, logWarning).

Examples

Example 1:

Input:  A `ConsoleLogger` instance and the message "Hello, world!"
Output: "Hello, world!" printed to the console.
Explanation: The `ConsoleLogger`'s `log` method receives the message and prints it using `console.log()`.

Example 2:

Input: A `FileLogger` instance and the message "This is a test."
Output: "Writing to file: This is a test." printed to the console.
Explanation: The `FileLogger`'s `log` method receives the message and prints a message indicating a file write.

Constraints

  • The code must be written in TypeScript.
  • The log method in both classes must accept a single string argument.
  • The solution should be well-structured and easy to understand.
  • No external libraries are allowed.

Notes

  • Think about how protocol types can help you enforce a contract between different parts of your application.
  • Consider the benefits of using protocol types for code reusability and maintainability.
  • TypeScript's compiler will be your friend here – leverage its type checking capabilities to ensure your implementation is correct. Pay close attention to any type errors you encounter.
  • The "file writing" simulation in FileLogger is purely for demonstration purposes; you don't need to actually write to a file.
Loading editor...
typescript