JS Built-ins
TypeScriptadvanced

Interface β€” Generic Container

πŸ“– Usage Example
interface Container<T> { map<U>(f: (x: T) => U): Container<U> }
const box: Container<number> = createBox(5)
box.map(x => x.toString())   // Container<string>

A generic Container interface with a map method models the Functor pattern from functional programming.

Fluent Generic Interface: Container<T>

Challenge: Implement createContainer<T>(value) returning a Container<T> with a map method for transformation.

createContainer(5).map(x => x * 2).value   // β†’ 10
createContainer('hi').map(s => s.length).value // β†’ 2
πŸ’‘ Hints (1)
  • β€’ map should call createContainer recursively to return a new Container.