Components
A TSRX component is just a TypeScript function that produces JSX. Use a statement-container body for component-shaped templates, especially when local setup, comments, scoped styles, or multiple rendered children belong with the markup.
In practice, components are ordinary TypeScript functions or const values. A component can use @{...}as the function body, giving you one
place for local state, derived values, template control flow, rendered elements, and
scoped styles.
1 export function Button({ label, onClick }: {
2 label: string;
3 onClick: () => void;
4 }) @{
5 <>
6 // Ripple, Preact, Solid, and Vue host elements:
7 <button class="btn" {onClick}>{label}</button>
8
9 // React host elements:
10 // <button className="btn" {onClick}>{label}</button>
11
12 <style>
13 .btn {
14 padding: 0.5rem 1rem;
15 border-radius: 4px;
16 }
17 </style>
18 </>
19 }Export them like any other function: export function Name() @{ <div /> }. The
compiler turns that into the right component shape for the target you're using.
When a bit of logic should stay plain JavaScript rather than render into the
template, put it in a normal function beside the markup. Use function fn() { ... }for ordinary control
flow, then call helpers from event handlers or expressions: onClick={fn}.
1 export function Counter() @{
2 let count = 0;
3
4 // Plain JS/TS control flow can live in nested functions.
5 function increment() {
6 if (count >= 10) {
7 count = 0;
8 } else {
9 count += 1;
10 }
11 }
12
13 <button onClick={increment}>Count: {count}</button>
14 }Statement containers
When a template scope mixes TypeScript setup with rendered output, wrap the setup in@{...}. TSRX treats everything before the final
renderable child as script, then the container must finish with exactly one output
node.
That final output can be a JSX element, a JSX fragment, or JSX control flow like @if, @for, @switch, or @try. It cannot be a bare expression container,
and no script statements can appear after it.
If the rendered part needs multiple siblings or text next to elements, wrap those children in a fragment so they become one output. The rule applies locally to component bodies, element children, and control-flow branches, so setup can stay close to the markup that uses it without turning ordinary template text into JavaScript.
Control-flow bodies are implicit statement containers too:@if,@for,@switch, and@tryarms all use{}blocks.
If you write setup statements and then a bare JSX element inside a normal{}function body, the compiler will ask you to add the missing@. Plain braces are JavaScript; statement-container
braces are@{...}.
1 function ProductCard({ product }: { product: Product }) @{
2 const name = product.name.trim();
3 const price = money(product.price);
4
5 <article>
6 <h2>{name}</h2>
7
8 <footer>@{
9 const hasDiscount = product.discount > 0;
10
11 <>
12 <strong>{price}</strong>
13 @if (hasDiscount) {
14 <span>Sale</span>
15 }
16 </>
17 }</footer>
18 </article>
19 }Prop shorthands
When a prop name matches the variable you're passing, you can use the shorthand{name}instead ofname={name}. This works for any attribute or
prop — including event handlers like{onClick}.
1 // Instead of repeating the name:
2 <Input value={value} onChange={onChange} />
3
4 // Use the shorthand:
5 <Input {value} {onChange} />Lexical scoping
Every statement container and control-flow block creates its own lexical scope. You can declare variables, compute derived values, or call functions there — they're scoped to that block and won't leak into the surrounding function.
1 function App() @{
2 const name = 'World';
3
4 <div>@{
5 // This is a new scope - you can declare variables here
6 const greeting = 'Hello, ' + name + '!';
7
8 <h1>{greeting}</h1>
9 }</div>
10 }This applies to all block-like contexts: statement containers and control flow
branches (@if,@for,@switch,@try). Each one has its own scope.
Conditional rendering
Use@if/else if/elsetemplate expressions directly inside templates, or return them directly from a
component. Branches render their template output, while ordinaryifstatements in setup stay plain JavaScript for guard returns. Directreturn,continue,
andbreakare not allowed inside@iftemplate branches.
1 function StatusBadge({ status }: { status: 'active' | 'idle' | 'offline' }) @{
2 @if (status === 'active') {
3 <span class="badge active">Online</span>
4 } @else if (status === 'idle') {
5 <span class="badge idle">Away</span>
6 } @else {
7 <span class="badge">Offline</span>
8 }
9 }List rendering
Render lists with@for (... of ...)loops. TSRX extends the syntax with optionalindexandkeyclauses so you don't need separate counters or key-extraction boilerplate. The
optional@emptybranch renders when the iterable has no items.
Filter the collection before passing it to@forwhen some items should not render. Use@emptyfor the no-items branch. Directcontinue,break,
andreturnstatements are not allowed in@fortemplate loop bodies; nested functions keep ordinary JavaScript control flow.
Other JavaScript loops are not template rendering constructs: regularfor,for...in,while, anddo...whileare rejected in TSRX template scope. Move imperative loops into a nested function or
effect, or render collections with@for (... of ...).
1 function TodoList({ items }: { items: Todo[] }) @{
2 const visibleItems = items.filter((item) => !item.hidden);
3
4 <ul>
5 @for (const item of visibleItems; index i; key item.id) {
6
JS comments
JavaScript line and block comments are valid between the children of any element in a
.tsrxfile: in a template, in a control-flow body, in JSX returned from a regular function, and in module-level JSX. They are comments, not rendered text, so you can annotate element bodies and control-flow branches without wrapping the note in braces. A comment renders like{/* … */}in TSX. A/*starts a comment anywhere in text. A//after whitespace, at the start of a line, or right after a tag starts a comment that runs to the end of the line, a closing tag included, so<p>a // note</p>on one line leaves the<p>unclosed. One that touches text, as inhttps://example.com, stays text.This differs from TSX, where
/* … */and a line starting with//between children are text, and{/* … */}is the only comment. TSX pasted into a.tsrxfile can render differently, most often a code sample in<pre>or<code>. To show text that looks like a comment, write its first/as the character reference/, or write the text as a string in braces: