function / @typed/template

parse

Parses a template string array (from a tagged template literal) into a reusable Template object.

This parser handles:

  • Standard HTML structure (elements, attributes, text, comments).
  • Custom part syntax for interpolation (e.g., ${value}).
  • Special attributes like .property, @event, ?boolean.
  • Self-closing tags and text-only elements (script, style).

Each call returns a new Template instance containing the static AST and dynamic part locations. Render targets cache their compiled DOM fragments or HTML chunks independently by template-literal identity.

Package version
1.0.0-beta.7
Category
Template parsing
Since
1.0.0

Import

import { parse } from "@typed/template";

Signatures

export declare function parse(template: ReadonlyArray<string>): Template.Template;

Why

One renderer-neutral AST gives DOM rendering, SSR, and hydration the same interpolation paths and template hash. Template syntax remains ordinary HTML with explicit prefixes for properties, events, booleans, data, spreads, and refs.

Ownership and lifetime

The parser reuses internal scratch storage synchronously but returns a fresh, caller-owned immutable model on every call. Render layers decide whether and how long to cache compiled results by template-literal identity.

Errors and trust

Malformed author templates throw parser errors during compilation. Parsing does not sanitize markup; dynamic values are escaped later by the HTML renderer according to the recorded part context.

Examples

import { parse } from "@typed/template/Parser"

// Each array boundary records one interpolation point. Runtime values are
// supplied later by a renderer, not passed to parse.
const template = parse([
  "<div id=\"",
  "\" class=\"container\"><p>Hello, ",
  "!</p></div>"
])

// Access parsed structure
console.log(template.nodes) // Array of parsed nodes
console.log(template.parts) // Array of dynamic interpolation points
console.log(template.hash) // Unique hash for the template

Other public imports

These import paths expose the same declaration. Each page retains its own public name and signature.

Source