TypeScript usage
Use DocStudio with TypeScript: bundled types, module resolution, exported template and diagnostic types, narrowing results and typing component stores.
DocStudio is written in strict TypeScript and ships its declarations. No extra @types package is needed.
Module resolution
The package is ESM only and has a subpath exports map. Both NodeNext and Bundler module resolution resolve every entry point:
{ "compilerOptions": { "module": "NodeNext", "moduleResolution": "NodeNext", "strict": true } }Typed templates
Template and the node types come from docstudio/core:
import type { Template, Diagnostic } from 'docstudio/core';
const template: Template = {
schemaVersion: '1.0.0',
body: [{ id: 'hello', type: 'text', content: ['Hello ', { path: 'name' }] }],
};renderDocument accepts unknown for the template and the data, because they usually come from storage or the network. It validates them for you.
Narrow the results
Results are plain objects, so use ordinary narrowing:
import { compile } from 'docstudio/core';
const compiled = compile({ template, data });
if (compiled.tree === undefined) {
throw new Error(compiled.diagnostics.map((item) => item.message).join('; '));
}
const tree = compiled.tree;The PDF service result is a discriminated union on ok:
const result = await service.render({ rdt: tree });
if (result.ok) {
result.pdf;
} else {
result.status;
result.diagnostics;
}Designer types
import type { DocumentPayload, DesignerProps } from 'docstudio/designer';DocumentPayload is { template, data }, the argument of onSubmit, onView and onPrint.
Browser-safe imports
docstudio, docstudio/core, docstudio/render and docstudio/pdf contain no Node built-ins. Import docstudio/server only from server code.