JS Built-ins
TypeScriptbeginner

Interface β€” Function as Argument

πŸ“– Usage Example
interface Predicate<T> { (x: T): boolean }
function keepIf<T>(arr: T[], pred: Predicate<T>): T[] {
  return arr.filter(pred)
}

Passing a typed function interface as an argument enforces the callback signature at compile time.

Function Types in Signatures

Challenge: Implement applyCallback<T, U>(value, callback) that applies callback to value and returns the result.

applyCallback(5, x => x * 2)            // β†’ 10
applyCallback('hello', s => s.length)  // β†’ 5
πŸ’‘ Hints (1)
  • β€’ Just call callback(value).