Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Generics

Higher-order generic functions in TypeScript

Reference for higher-order generic functions in TypeScript: functions that take or return generic functions, compose, curry and keep types through wrappers.

A higher-order function takes a function as an argument, returns a function, or both. When such functions are generic, the type parameters let the wrapper preserve the exact signature of whatever it wraps: the parameters it accepts, the type it returns, and how those change along the way.

Typical examples are compose, pipe, memoize, debounce, curry and once. The key tools are function types with type parameters, the Parameters<F> and ReturnType<F> utility types, and, since TypeScript 3.4, higher-order inference that carries a generic function's type parameters through to the result.

TypeScript
function once<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R {
  let called = false;
  let result!: R;
  return (...args) => {
    if (!called) { result = fn(...args); called = true; }
    return result;
  };
}

Try it

TypeScript Wrappers that keep the signature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Compose, pipe and curry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
function wrap<A extends unknown[], R>(fn: (...args: A) => R): (...args: A) => R;
function wrap2<F extends (...args: any[]) => any>(fn: F): (...args: Parameters<F>) => ReturnType<F>;
function returnsGeneric(): <T>(value: T) => T;
Piece Description
(...args: A) => R with A extends unknown[] Captures a whole parameter list as a tuple type, preserving names, optionality and arity.
F extends (...args: any[]) => any Captures the whole function type; use with Parameters<F> and ReturnType<F>.
<T>(x: T) => T as a return type A generic function value: each call to it picks its own T.

Edge cases: Parameters<F> and ReturnType<F> on an overloaded function only see the last overload. A generic function passed through a wrapper keeps its type parameters only when the wrapper's signature is a single generic call signature; otherwise the type parameters become unknown.

Usage notes

  • Prefer the A extends unknown[] form over any[]: it keeps tuple labels and optional parameters, which improves editor hints.
  • this is dropped unless you declare a this parameter on the wrapper and forward it with fn.apply(this, args).
  • Overloads are lost. A wrapper sees only one signature. For overloaded APIs write the wrapper's overloads by hand.
  • Currying helps inference. Splitting parameters across calls lets the first call fix T so later arguments are checked against it.
  • Return a generic function when the caller decides T; return a non-generic function when you decide T up front.
  • Async wrappers should wrap the return type explicitly: Promise<Awaited<ReturnType<F>>>.
  • Type-only reasoning: none of these types affect the emitted JavaScript, so a wrong cast inside the wrapper (as R) compiles but is not checked.