tshtml
    Preparing search index...

    Class JsExpression<T>

    Wraps a JavaScript value to be rendered as JavaScript code in HTML templates. Handles proper serialization of objects, arrays, primitives, and raw JavaScript strings.

    import { JsExpression } from 'tshtml';

    // In template attributes
    div({
    onclick: new JsExpression('handleClick(event)'),
    dataset: new JsExpression({ id: 123, name: 'test' })
    })

    Type Parameters

    • T = any

      The type of the wrapped value

    Implements

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    value: JsonLike<T>

    The JSON-compatible value wrapped by this expression

    Methods

    • Renders the JavaScript expression as a string suitable for HTML attributes or script tags. String values are treated as raw JavaScript expressions (without quotes). Other values are JSON-serialized.

      Parameters

      • isAttributeValue: boolean

        Whether this is being rendered as an HTML attribute value

      Returns string

      The JavaScript code representation of the value

      expr('getValue()').render(true);      // 'getValue()'
      expr({ x: 1 }).render(true); // '{x:1}'
      expr([1, 2, 3]).render(true); // '[1,2,3]'