Building reusable document templates in TypeScript
Structure DocStudio templates for reuse: named styles, page furniture, versioned components with props and slots, and a typed pipeline in TypeScript.
A template that works once is easy. A set of templates that stay consistent for years needs structure. DocStudio gives you three levels of reuse.
Level 1: named styles
Define a style once and reference it with styleRef:
{
"schemaVersion": "1.0.0",
"styles": {
"title": { "fontSize": 24, "fontWeight": "bold", "color": "#1e3a5f" },
"muted": { "color": "#6b7280" }
},
"body": [
{ "id": "heading", "type": "text", "content": ["Invoice"], "styleRef": "title" },
{ "id": "note", "type": "text", "content": [{ "path": "note" }], "styleRef": "muted" }
]
}Styles are whitelisted properties, so there is no free-form CSS to drift. A brand change is one edit.
Level 2: shared page furniture
Headers and footers are the same across a family of documents. Keep them in one TypeScript module and merge them into each template:
import type { Template } from 'docstudio/core';
export const furniture: Pick<Template, 'page' | 'styles'> = {
page: {
footer: {
height: 12,
children: [{ id: 'pages', type: 'pageNumber', format: 'Page {n} of {total}' }],
},
},
styles: { muted: { color: '#6b7280' } },
};
export function invoiceTemplate(): Template {
return { schemaVersion: '1.0.0', ...furniture, body: [] };
}Because a template is data, plain object composition is enough.
Level 3: versioned components
A reusable component is a template with props and slots, imported by name and version:
{
"id": "top",
"type": "component",
"ref": "letterhead@1.0.0",
"props": { "organisation": "Northwind Clinic" },
"slots": { "extra": [{ "id": "note", "type": "text", "content": ["Open weekdays"] }] }
}The component reads $props.organisation and marks the slot with a slot node. You provide a store that resolves names and versions:
import { compile, type ComponentStore } from 'docstudio/core';
const store: ComponentStore = {
get(name, version) {
return name === 'letterhead' && version === '1.0.0' ? { template: letterhead } : undefined;
},
};
const result = compile({ template, data, components: store });Why versions matter
A published version is immutable and identified by name, version and content hash. Documents pin the version, so shipping letterhead@1.1.0 does not change any document that pinned 1.0.0. Moving to the new version is an explicit step, and in the designer it shows an impact preview and can be undone.
Keep the contract typed
Accept unknown where data enters your system, and let DocStudio validate it. Keep your own domain types around the template-building functions. Check templates in CI with validateTemplateStructure so a bad template fails a build rather than a request:
import { validateTemplateStructure } from 'docstudio/core';
const check = validateTemplateStructure(template);
if (!check.valid)
throw new Error(check.diagnostics.map((item) => `${item.path}: ${item.message}`).join('\n'));Trade-offs
Components add a store to maintain and versions to reason about. Use them for parts shared by many templates, such as a letterhead or a signature block. For a single family, level 1 and 2 are enough.
See template reuse and the templates concept.
Related: the matching feature page and the quick start.