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 ==="