Skip to main content

decorate()

decorate<In, Out>(fn, ...decorators): (input) => Out

Applies one or more decorators to a function. Decorators are applied left-to-right: the first decorator is the innermost wrapper, the last is the outermost.


Type Parameters​

In: In​

The input type

Out: Out​

The output type


Parameters​

fn: (input) => Out​

The function to decorate

decorators: ...Decorator<In, Out>[]​

Decorators to apply


Returns​

The decorated function


Since​

2.4.0


Example​

const add = (n: number) => n + 1;

const enhanced = decorate(
add,
before((n) => console.log("input:", n)),
after((_n, result) => console.log("output:", result)),
);

enhanced(5); // logs: input: 5, output: 6, returns 6

Decorator()<In, Out>​

Type

Decorator<In, Out> = (fn) => (input) => Out

A Decorator is a higher-order function that wraps a function, adding behavior while preserving its signature. Replaces the GoF abstract Decorator class + inheritance chain.


Type Parameters​

In: In​

The input type

Out: Out​

The output type


Parameters​

fn: (input) => Out​


Returns: (input): Out​


Since​

2.4.0