ReactiveScript
A small language for reactive UIs. It carries Solid 2.0's reactive semantics as language semantics, and checks them before your code runs.
Experimental · 0.3. Files are .rx and compile to plain Solid 2.0. Releases follow Solid 2.0's release candidates, so expect breaking changes until Solid 2.0 is final.
Mistakes of shape become errors
In Solid you already write UIs as derivations over signals. Most of what goes wrong is a shape mistake: writing to something that should be derived, writing during a computation, reading async data with no loading or error boundary above it. ReactiveScript makes those things the checker refuses, the way Rust refuses a borrow.
Three kinds of names do the work. A source is state, and only a source is written, with :=. An equation, y = x * 2, is always true and recomputes when what it reads changes. Writes happen only in an event handler or an action.
counter.rxexport default component Counter() {
source count := 0
double = count * 2
<button onClick={() => { count := count + 1 }}>{count} × 2 = {double}</button>
}
Write to the equation instead, double := 4, and the checker refuses it, in your editor and in CI:
counter.rx:5:3 - error WRITE_TO_NON_SOURCE: double is an equation: only sources are written (W-1)
Destructure props, keep them live
In Solid, destructuring props reads them once and the component stops updating. In .rx, destructure them right in the parameter list: every name follows its prop, defaults and rest included, and the rest spreads onto an element as in JSX.
inbox.rxcomponent Badge({ label, tone = "info", ...rest }: { label: string; tone?: string; title?: string }) {
<span class={tone} {...rest}>{label}</span>
}
export default component Inbox() {
source user := { name: "Ada", unread: 3 }
<Badge label={`${user.unread} unread`} title={user.name} /> // Badge updates when user changes
<button onClick={() => { user := { ...user, unread: 0 } }}>Mark read</button>
}
Async is just await
Put an await in an equation. The checker tracks that the value can be pending and can fail, and asks you to say what to show in both cases.
profile.rximport pure { fetchUser } from "./api"
export default component Profile() {
source id := 1
user = await fetchUser(id) // re-asked whenever `id` changes
<section>
<button onClick={() => { id := id + 1 }}>Next</button>
{loading fallback { <p>Loading…</p> } {
<h1>{user.name}</h1>
}}
</section>
}
There's a loading boundary but no errored one, so the checker says what's missing and how to fix it:
profile.rx:10:7 - error UNHANDLED_FAILURE: a reader of user.name (in <Profile>) can fail with Error and no catch or errored boundary above it discharges Error — wrap it in `errored fallback(e) { … } { … }`, or catch it: `… catch (e) => …` (R-f-5)
A like button, the first time you write it
Show the new count at once, send it, and let the server's answer replace the guess. That's guess, which lowers to Solid's createOptimistic. This is how most people write it first:
like.rximport pure { fetchLikes } from "./api"
import { like } from "./api"
export default component Like(props: { id: number }) {
likes = await fetchLikes(props.id)
action send() {
guess likes := likes + 1 // show it now; the server's answer replaces it
await like(props.id)
refresh likes
}
<button onClick={() => { send() }}>♥ {likes}</button>
}
It has three bugs, and two only show when the network is slow or a request fails. The checker finds all three:
like.rx:13:3 - error UNHANDLED_FAILURE: a reader of likes (in <Like>) can fail with Error and no catch or errored boundary above it discharges Error — wrap it in `errored fallback(e) { … } { … }`, or catch it: `… catch (e) => …` (R-f-5)
like.rx:7:3 - error UNCAUGHT_ACTION_FAILURE: action send can fail with Error and does not catch it: a handler cannot (it does not await the action) — `try { … } catch (e) { … }` in the body (R-f-6)
like.rx:13:3 - error GUESS_WHILE_LOADING: <button> onClick starts `send`, which can run `guess likes` while likes is loading a new question (its question reads what the click reads): the guess composes on the previous answer, and the new answer replaces it, so the change disappears while it is out (D-34) — put the element inside the `loading` region whose content reads likes, or give it `disabled={pending? likes}`, or keep the pending change in a source of its own until it lands (AC-3a)
The last one is the subtle one. Click while the count is still reloading and your +1 lands on the old answer, then vanishes when the new one arrives. Each error names the fix, and the fixed version passes:
like.rxexport default component Like(props: { id: number }) {
likes = await fetchLikes(props.id) catch (e) => 0
action send() {
guess likes := likes + 1
try {
await like(props.id)
refresh likes
} catch (e) {} // the guess goes away by itself
}
{loading fallback { <p>Loading…</p> } {
<button onClick={() => { send() }}>♥ {likes}</button>
}}
}
Races, refused
The bugs that come from await in a handler don't show in a demo. They show when the network is slow or two clicks overlap.
The "Saving…" that never shows
A handler that writes and awaits is an action, and an action's writes land together when it ends. So this status only ever reads the result.
<button onClick={() => {
status := "Saving…"
let r = await saveName("Ann") catch (e) => e.message
status := r
}}>{status}</button>
save.rx:5:3 - error HELD_WRITE: status := … comes before an `await` in a body that is an action because it writes and awaits (AC-1b), so it is held until the body ends (AC-1): for state that shows while the work runs, `guess status := …` (a source with a question, ST-5c); for a reset that lands now, `onFlush(() => { status := … })` (EF-8); to hold it on purpose, declare the action (`action name() { … }`); or write it after the `await` (AC-1c)
The write that undoes another click
Read, await, write back. If anything changed tags in between, this overwrites it.
action add(t: string) {
let before = tags
let saved = await addTag(t) catch (e) => t
tags := before + " " + saved
}
tags.rx:5:3 - error STALE_WRITE_BACK: tags := … after an `await` is computed from tags as it was read before that `await`: whatever changed tags in between (another action, a reload) is overwritten — write functionally (`tags := v => …`, W-6), which applies to tags as it stands at the write, or guess it (`guess`, AC-3) and let the answer replace the guess (AC-1e)
Every word is a Solid primitive
A .rx program compiles to ordinary Solid 2.0 and adds nothing at run time beyond what Solid does. Each form in the language has one counterpart:
| .rx | Solid 2.0 |
|---|---|
source x := 0 | createSignal, or createStore for objects |
y = x * 2 | createMemo, createProjection |
user = await fetchUser(id) | an async createMemo |
guess likes := likes + 1 | createOptimistic |
component C({ label, ...rest }) | props.label, omit(props, "label") |
action send() { … } | action |
effect (x) { … } | createEffect(compute, apply) |
loading / errored | <Loading> / <Errored> |
Static checks only
The checks run in your editor and in CI. The compiled code is Solid's, with Solid's runtime behaviour.
Easy for an AI to write
The rules are small and explicit. Every error names the rule it enforces and usually the fix, so an agent can correct itself.
TypeScript underneath
Expressions and JSX are TypeScript's, and types are checked by TypeScript in the editor and in CI.
The spec is the product
Every behaviour is a numbered rule or an executable scenario, checked against Solid itself.
Use any Solid library
A Solid library is plain JavaScript, and .rx can use any of them: solid-primitives, a design system's hooks, your own create* helpers. Declare what you use once, in a .d.rx beside it, and the checker holds every use to that declaration. A ref is an accessor: you pass and read plain values, and the compiler adds the calls.
media.d.rx// beside ./media.js, which re-exports it from @solid-primitives/media
export pure fn createMediaQuery(query: string): ref boolean
nav.rximport { createMediaQuery } from "./media.js"
export default component Nav() {
wide = createMediaQuery("(min-width: 768px)") // runs once; read it like any equation
<nav>{if wide { <Links /> } else { <Menu /> }}</nav>
}
A package can ship its own .d.rx beside its build, and then there's nothing to declare. See Using Solid libraries.
Install
In a Solid app (Vite). Install the compiler and the runtime beside Solid 2.0. Solid's Vite plugin (3.0.0-next.57 or later) finds the compiler and compiles .rx on its own, so vite.config.js stays as it is.
npm i solid-js@next @solidjs/web@next @solidjs/signals@next @reactivescript/runtime
npm i -D vite @solidjs/vite-plugin @reactivescript/compiler
src/main.jsimport { createApp } from "@reactivescript/runtime";
import main from "./app.rx";
const app = createApp({ root: document.getElementById("app") });
app.mount(main(app));
In your editor. The ReactiveScript extension brings highlighting, the checker's errors, and TypeScript's hover, completion, rename and type errors on .rx. Get it from the VS Code Marketplace, or from Open VSX for Cursor and VSCodium. Other editors can run npx @reactivescript/language-server --stdio.
In CI. Vite compiles .rx without checking it, as it does .ts. rx-tsc is tsc with .rx in the program: the same check the editor runs, with every error above.
npm i -D @reactivescript/language-server typescript
npx rx-tsc --noEmit
Learn more
- The language on one page : the whole working language
- Talking to a server : async, guesses and what waits
- Solid libraries : declare a library once in a
.d.rx - Diagnostics : every error code, why, and the fix
- The spec : the language as numbered rules
- Examples : real apps in
.rx - @reactivescript/compiler on npm : the Rust compiler, with a Vite front end