Setup
SAFE-TS for React uses the same Rust checker and build transform as every SAFE-TS project (Adopting SAFE-TS covers tsconfig.json and oxlint), plus @evobug_com/safe-ts-react for React 18/19 component helpers.
In short
- Install the React helpers: add
@evobug_com/safe-ts-reactalongside@evobug_com/safe-ts-std,@evobug_com/safe-ts-transform, and the oxlint plugin. - Plugin order matters: register
safeTs(...)beforereact()invite.config.tssoq(...),match(...), andparseJson<T>rewrite before JSX compilation. - Full coverage: Vite dev/build,
safe-ts check, and oxlint inspect.tsxcomponents and custom hooks in.tsfiles importingreact.
sh
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-reactRegister safeTs before @vitejs/plugin-react so SAFE-TS checks and rewrites each file prior to React's JSX transform:
ts
// vite.config.ts
import { safeTs } from "@evobug_com/safe-ts-transform";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig({ plugins: [safeTs({ check: "error" }), react()] });See examples/react/ for a complete working app.
Where the checks run:
.tsx files | Hooks (.ts files importing react) | |
|---|---|---|
| Vite dev server and build | ✅ | ✅ |
safe-ts check src/main.tsx | ✅ | ✅ |
| Editor (oxlint plugin) | ✅ | ✅ |
| Type checking | tsc | tsc |
The React rules run on every .tsx and .jsx file, and on .ts files that import from react (custom hooks). In the oxlint preset they are errors.
Use it with eslint-plugin-react-hooks
SAFE-TS leaves to eslint-plugin-react-hooks what it already does well: the rules of hooks, effect dependencies, and (in its recommended preset) state set or changed in place during render. The JSX key rule of your linter (react/jsx-key, or oxlint's) stays too. The SAFE-TS rules don't repeat them.
How JSX is read
JSX is code, and the rules you know apply in it as they do anywhere else:
✕ Rejectedunhandled-failureno-null
tsx
export function Settings({ settings }: { settings: string }) {
return (
<>
<pre>{JSON.stringify(JSON.parse(settings), undefined, 2)}</pre>
{settings === "" ? null : <p>{settings.length} characters</p>}
</>
);
}Event props (onClick, onSubmit, any on and a capital letter) are functions React calls later, with nothing of yours around them:
| JSX | Read as |
|---|---|
{e}, value={e} | e, run on every render |
onClick={save} | save(event) in a function React calls later; what it returns is dropped |
onClick={() => save()} | that function, called later; what it returns is dropped |
onClick={() => { save(); }} | that function, called later |
<Form onSubmit={save}> | called later too; a component may use what it returns, so only DOM elements drop it |
So a Result returned to an event prop is must-use-result, and a PropagateError leaving a handler is uncaught-propagation: Errors in handlers.