0 of 5 problems solved0%
1 of 5

Identity & Box

A generic function uses a type parameter to connect the parameter type and the return type. When you pass a value, TypeScript often infers the type argument for you. If you call it with a string, you get a string back; if you call it with a number, you get a number back.

You can also create a generic interface like Box<T> that wraps a value and preserves its type. The T acts like a variable for the type, so that it can be used for any type the user wants to pass in.

In this example, the wrap function can wrap any type in an object with a single property named value.

function wrap<T>(value: T) {
  return { value };
}

Your Task

Implement a generic identity and a generic box.

  • Define interface Box<T> { value: T }.
  • Implement function identity<T>(value: T): T that returns the value unchanged.
  • Implement function boxOf<T>(value: T): Box<T> that returns { value }.
  • Keep implementations minimal and do not mutate inputs.