Skip to main content

templateWithDefaults()

templateWithDefaults<Steps, Fn>(skeleton, defaults): (overrides?) => Fn

Creates a template method with default steps that can be partially overridden.

This is the functional equivalent of an abstract class with hook methods that have default implementations. Only the steps you want to customize need to be provided.


Type Parameters​

Steps: Steps extends object​

The record type describing the customizable steps

Fn: Fn​

The resulting algorithm function type


Parameters​

skeleton: (steps) => Fn​

A function that wires the steps into an algorithm

defaults: NoInfer<Steps>​

Default implementations for all steps


Returns​

A function that accepts partial step overrides and returns the algorithm


Since​

2.4.0


Example​

const report = templateWithDefaults(
(steps: {
header: () => string;
body: (data: string) => string;
footer: () => string;
}) =>
(data: string) => [steps.header(), steps.body(data), steps.footer()].join("\n"),
{
header: () => "=== Report ===",
body: (data) => data,
footer: () => "=== End ===",
},
);

// Only override what you need
const custom = report({ header: () => "** Custom **" });
custom("content"); // "** Custom **\ncontent\n=== End ==="