JSX is executable code: JSON.parse during render can throw, onClick={save} silently discards a returned Result, and an uncancelled useEffect fetch overwrites fresh data with stale responses. SAFE-TS checks .tsx components and custom hooks statically—before anything mounts.
React Architecture
Every SAFE-TS rule applies inside JSX: unhandled throws, dropped Results, and null. Event props are callbacks React invokes later with no caller around them.
Props belong to the parent. Read them; notify changes through callbacks the parent passes down. Mutating a prop object in place fails no-param-mutation.
Replace separate loading, error, and data states that can contradict each other with one AsyncState. useTask aborts superseded runs automatically.
Every listener, interval, or request an effect starts must be torn down when dependencies change or the component unmounts—especially under StrictMode double-mounts.
Side-by-Side · Checked with tsc --strict
Every pair below is a real .tsx component verified by cargo test and tsc --strict.
React ignores what event handlers return, so the Result from publish is silently lost.
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
function publish(draft: string): Result<void, PublishError> {
if (draft === "") return err(PublishError.Empty());
if (draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
return undefined;
}
export function Editor({ draft }: { draft: string }) {
return <button onClick={() => publish(draft)}>Publish</button>;
}import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
function publish(draft: string): Result<void, PublishError> {
if (draft === "") return err(({ tag: "Empty" } satisfies PublishError as PublishError));
if (draft.length > 280) return err(({ tag: "TooLong", max: 280 } satisfies PublishError as PublishError));
return undefined;
}
export function Editor({ draft }: { draft: string }) {
return <button onClick={() => publish(draft)}>Publish</button>;
}Store component state in one Enum (Editing, Rejected, Published) and handle every variant with <Match>.
import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-react/match";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
type Publishing = Enum<{ Editing: {}; Rejected: { error: PublishError }; Published: {} }>;
const Publishing = constructors<Publishing>();
function check(draft: string): Result<void, PublishError> {
if (draft === "") return err(PublishError.Empty());
if (draft.length > 280) return err(PublishError.TooLong({ max: 280 }));
return undefined;
}
export function Editor({ draft, onPublish }: { draft: string; onPublish: (text: string) => void }) {
const [state, setState] = useState<Publishing>(Publishing.Editing());
const publish = (): void => {
const checked = check(draft);
if (isErr(checked)) {
setState(Publishing.Rejected({ error: checked.error }));
return;
}
onPublish(draft);
setState(Publishing.Published());
};
const button = <button onClick={publish}>Publish</button>;
return (
<Match
on={state}
Editing={() => button}
Rejected={({ error }) => (
<>
{button}
<Match
on={error}
Empty={() => <p>Write something first.</p>}
TooLong={({ max }) => <p>Keep it under {max} characters.</p>}
/>
</>
)}
Published={() => <p>Published.</p>}
/>
);
}import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-react/match";
type PublishError = Enum<{ Empty: {}; TooLong: { max: number } }>;
const PublishError = constructors<PublishError>();
type Publishing = Enum<{ Editing: {}; Rejected: { error: PublishError }; Published: {} }>;
const Publishing = constructors<Publishing>();
function check(draft: string): Result<void, PublishError> {
if (draft === "") return err(({ tag: "Empty" } satisfies PublishError as PublishError));
if (draft.length > 280) return err(({ tag: "TooLong", max: 280 } satisfies PublishError as PublishError));
return undefined;
}
export function Editor({ draft, onPublish }: { draft: string; onPublish: (text: string) => void }) {
const [state, setState] = useState<Publishing>(({ tag: "Editing" } satisfies Publishing as Publishing));
const publish = (): void => {
const checked = check(draft);
if (isErr(checked)) {
setState(({ tag: "Rejected", error: checked.error } satisfies Publishing as Publishing));
return;
}
onPublish(draft);
setState(({ tag: "Published" } satisfies Publishing as Publishing));
};
const button = <button onClick={publish}>Publish</button>;
return (
<Match
on={state}
Editing={() => button}
Rejected={({ error }) => (
<>
{button}
<Match
on={error}
Empty={() => <p>Write something first.</p>}
TooLong={({ max }) => <p>Keep it under {max} characters.</p>}
/>
</>
)}
Published={() => <p>Published.</p>}
/>
);
}More in Errors in handlers.
Mutates the parent's user object directly—React won't re-render for it, and the parent's snapshot goes stale.
type User = { name: string; email: string };
export function EmailField({ user }: { user: User }) {
return <input value={user.email} onChange={(e) => { user.email = e.target.value; }} />;
}Pass the value in and notify updates through a callback so the parent remains the single owner.
export function EmailField({ email, onEmail }: { email: string; onEmail: (email: string) => void }) {
return <input value={email} onChange={(e) => onEmail(e.target.value)} />;
}More in State and props.
Three states that can contradict each other, plus an uncancelled effect where a slow response overwrites a newer selection.
import { useEffect, useState } from "react";
import { isErr } from "@evobug_com/safe-ts-std/result";
import { fetchUser, type User } from "./api";
export function Profile({ id }: { id: string }) {
const [loading, setLoading] = useState(false);
const [failed, setFailed] = useState("");
const [user, setUser] = useState<User>();
useEffect(() => {
const load = async (): Promise<void> => {
setLoading(true);
const result = await fetchUser(id);
setLoading(false);
if (isErr(result)) setFailed(result.error.message);
else setUser(result);
};
void load();
}, [id]);
if (loading) return <p>Loading…</p>;
if (failed !== "") return <p>{failed}</p>;
return <p>{user?.name}</p>;
}One AsyncState value rendered through <Match>, with useTask aborting the previous request on every run(id).
import { useEffect } from "react";
import { Match } from "@evobug_com/safe-ts-react/match";
import { useTask } from "@evobug_com/safe-ts-react/task";
import { fetchUser } from "./api";
export function Profile({ id }: { id: string }) {
const { state, run } = useTask((signal, id: string) => fetchUser(id, signal));
useEffect(() => {
run(id).catch((reason: unknown) => console.error(reason));
}, [id, run]);
return (
<Match
on={state}
Loading={() => <p>Loading…</p>}
Ready={({ value }) => <p>{value.name}</p>}
Failed={({ error }) => <p>Couldn't load the user: {error.message}</p>}
_={() => undefined}
/>
);
}More in Async state.
The resize listener is never removed—every re-run and StrictMode double-mount leaks another listener.
import { useEffect, useState } from "react";
export function Width() {
const [width, setWidth] = useState(0);
useEffect(() => {
const measure = (): void => setWidth(window.innerWidth);
window.addEventListener("resize", measure);
}, []);
return <p>{width}px</p>;
}Return a cleanup function that removes the exact listener registered by the effect.
import { useEffect, useState } from "react";
export function Width() {
const [width, setWidth] = useState(0);
useEffect(() => {
const measure = (): void => setWidth(window.innerWidth);
window.addEventListener("resize", measure);
return () => window.removeEventListener("resize", measure);
}, []);
return <p>{width}px</p>;
}More in Cleanup.
dangerouslySetInnerHTML injects untrusted markup straight into the DOM.
type Comment = { readonly author: string; readonly body: string };
export function CommentView({ comment }: { comment: Comment }) {
return (
<article>
<h3>{comment.author}</h3>
<div dangerouslySetInnerHTML={{ __html: comment.body }} />
</article>
);
}Render untrusted strings as children so React escapes them automatically (or require a sanitized Html brand).
type Comment = { readonly author: string; readonly body: string };
export function CommentView({ comment }: { comment: Comment }) {
return (
<article>
<h3>{comment.author}</h3>
<p>{comment.body}</p>
</article>
);
}More in Security.
Tooling & Setup
| Surface | .tsx components | Custom hooks (.ts importing react) |
|---|---|---|
| Vite dev server & build | ✅ | ✅ |
safe-ts check src/main.tsx | ✅ | ✅ |
| Editor & CI (oxlint plugin) | ✅ | ✅ |
| Type checking | tsc | tsc |
SAFE-TS complements—not replaces—eslint-plugin-react-hooks (rules of hooks, dependency arrays) and your linter's JSX key rule.
npm install -D @evobug_com/safe-ts-transform @evobug_com/oxlint-plugin-safe-ts oxlint
npm install @evobug_com/safe-ts-std @evobug_com/safe-ts-reactimport { safeTs } from "@evobug_com/safe-ts-transform";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
// safeTs before react: checks each file and rewrites q()/match() before React compiles JSX.
export default defineConfig({ plugins: [safeTs({ check: "error" }), react()] });git clone https://git.evobug.com/Evobug/Safe-Ts.git
cd Safe-Ts
npm install
npm run build
npm run dev -w examples/reactFull tsconfig, oxlint, and prelude setup is in React Setup.