Hone logo
Hone
Problems

Parameter Decorators in TypeScript: Logging Function Arguments

Parameter decorators in TypeScript allow you to modify or observe the values of function parameters before a function is executed. This challenge asks you to implement a parameter decorator that logs the name and value of each parameter passed to a decorated function. This is a useful technique for debugging, logging, and potentially validating function arguments.

Problem Description

You need to create a TypeScript decorator factory called logParams that, when applied to a function, logs the name and value of each parameter passed to that function to the console before the function executes. The decorator should handle functions with any number of parameters, including functions with no parameters. The logging should be clear and informative, indicating the function name and the parameter details.

Key Requirements:

  • Decorator Factory: The solution must be a decorator factory (a function that returns a decorator). This allows for potential customization (though customization isn't required for this specific problem).
  • Parameter Logging: The decorator must log the name and value of each parameter to the console.
  • Function Name: The log messages should include the name of the decorated function.
  • Execution Timing: Logging must occur before the decorated function is executed.
  • Handles Various Parameter Counts: The decorator must work correctly regardless of the number of parameters the decorated function accepts.
  • Handles No Parameters: The decorator must work correctly for functions with no parameters.

Expected Behavior:

When a function decorated with logParams is called, the following should happen:

  1. The decorator should intercept the function call.
  2. It should log a message to the console for each parameter, including the function name, parameter name, and parameter value.
  3. Then, it should execute the original function with the provided arguments.
  4. The return value of the original function should be returned by the decorated function.

Edge Cases to Consider:

  • Functions with no parameters.
  • Functions with a large number of parameters.
  • Parameters with complex data types (objects, arrays, etc.).
  • The decorator should not modify the original function or its parameters.

Examples

Example 1:

Input:
function add(x: number, y: number): number {
  return x + y;
}

const loggedAdd = logParams(add);

loggedAdd(5, 3);
Output:
"add: x = 5"
"add: y = 3"
6

Explanation: The logParams decorator is applied to the add function. When loggedAdd(5, 3) is called, the decorator logs "add: x = 5" and "add: y = 3" to the console before executing the add function, which returns 6.

Example 2:

Input:
function greet(name: string): void {
  console.log(`Hello, ${name}!`);
}

const loggedGreet = logParams(greet);

loggedGreet("Alice");
Output:
"greet: name = Alice"
Hello, Alice!

Explanation: The logParams decorator is applied to the greet function. When loggedGreet("Alice") is called, the decorator logs "greet: name = Alice" to the console before executing the greet function.

Example 3:

Input:
function noParams(): void {
  console.log("No parameters here!");
}

const loggedNoParams = logParams(noParams);

loggedNoParams();
Output:
(No output before function execution)
No parameters here!

Explanation: The logParams decorator is applied to the noParams function. When loggedNoParams() is called, the decorator does not log anything because there are no parameters. The noParams function is then executed, printing "No parameters here!".

Constraints

  • The solution must be written in TypeScript.
  • The decorator factory must accept no arguments.
  • The logging should be done using console.log.
  • The solution should be reasonably efficient; excessive logging operations should be avoided.
  • The solution must not modify the original function or its parameters.

Notes

  • Remember that parameter decorators are a relatively new feature in TypeScript. Ensure your TypeScript version supports them (TypeScript 3.9 or later).
  • The ParameterDecorator interface is key to defining your decorator.
  • Consider using the Reflect.get method to access parameter values.
  • Think about how to access the function name within the decorator. function.name is a useful property.
  • The decorator factory allows for future expansion, such as adding a custom logging function or formatting options. While not required for this problem, consider how your solution could be extended.
Loading editor...
typescript