Skip to content

tap

tap<K, T>(fn: T[K]): Funcware<MethodOf<HasMethod<K>, K>>

Defined in: ext/funcware/tap.ts:29

Creates funcware that taps into a method call, running a side-effect function after the original method.

Use cases:

  • Logging, analytics, or debugging after method execution
  • Triggering side effects without altering the original return value
  • Composing additional behavior in functional pipelines
Type ParameterDescription
K extends stringThe method name
T extends HasMethod<K>The target type with the method
ParameterTypeDescription
fnT[K]The function to run after the original method

Funcware<MethodOf<HasMethod<K>, K>>

Funcware that runs the side-effect function after the original method

This function is useful for scenarios where you want to observe or react to method calls without changing their result, such as for logging, metrics, or triggering external effects.

export const tap =
<K extends string, T extends HasMethod<K>>(
fn: T[K]
): Funcware<MethodOf<HasMethod<K>, K>> =>
(inner) =>
(...params) => {
const res = inner(...params);
fn(...params);
return res;
};
const logParams = (...params: any[]) => console.log('Called with:', ...params);
const tapware = tap(logParams);
const original = (x: number) => x * 2;
const tapped = tapware(original);
tapped(5); // Logs 'Called with: 5', returns 10