Reintent

Installation

Add Reintent to a TypeScript project, and write intents next to React views.

Reintent is a library for TypeScript. Intents are written in Reintent's JSX, in files of their own, named .intent.tsx. Views and everything else keep their own JSX.

Install

npm install @reintent/core

To read intents from React views, add the React bindings, for React 19:

npm install @reintent/react @reintent/dom react react-dom

Intent files

A compiler reads each file with one JSX. <Card /> becomes a call to one intent, and TypeScript checks it against that intent's types. So intents and React views live in separate files, and the file name tells them apart:

FileJSXAPIs
reply.intent.tsxReintent's: lowercase tags of the DOM catalog (@reintent/dom) and componentsuseChoose, useGenerate, useContext from @reintent/core
App.tsxReact's: <div>, <button>, componentsReact's, and reintentView, ReintentProvider, useReintent from @reintent/react

Name every file written in Reintent's JSX .intent.tsx, in a project with React views and in one without. Its tests are .intent.test.tsx, which test runners pick up as tests.

A project with only intents

When every .tsx file in a project is an intent (a host of its own, a server, tests), set Reintent's JSX for the whole project:

tsconfig.json
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "@reintent/core"
  }
}

Core's JSX writes components and host(…). For the DOM catalog's lowercase tags, set jsxImportSource to @reintent/dom.

esbuild and tsx read the same tsconfig.json. With Babel or SWC, set the automatic intent's import source to @reintent/core: importSource in @babel/preset-react, jsc.transform.react.importSource in SWC.

A project with React views

Intent files and views compile with different JSX, so every tool has to know which file is which. TypeScript and Vite learn it from the TypeScript projects; esbuild, tsx, Babel and SWC from a line at the top of each intent file. The views then read the intents through an intent.

TypeScript and Vite

Give the intent files a TypeScript project of their own. Vite's React template already splits tsconfig.json into projects (tsconfig.app.json, tsconfig.node.json); add one more, for the files that end in .intent.tsx:

tsconfig.json
{
  "files": [],
  "references": [
    { "path": "./tsconfig.intent.json" },
    { "path": "./tsconfig.app.json" },
    { "path": "./tsconfig.node.json" }
  ]
}
tsconfig.intent.json
{
  "compilerOptions": {
    // The same options as tsconfig.app.json, and:
    "composite": true,
    "emitDeclarationOnly": true,
    "outDir": "./node_modules/.tmp/intent",
    "jsx": "react-jsx",
    // The DOM catalog's entry: its kinds as lowercase tags, on intent JSX.
    "jsxImportSource": "@reintent/dom"
  },
  "include": ["src/**/*.intent.tsx", "src/**/*.intent.test.tsx"]
}
tsconfig.app.json
{
  "compilerOptions": {
    // …
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": ["src/**/*.intent.tsx", "src/**/*.intent.test.tsx"],
  "references": [{ "path": "./tsconfig.intent.json" }]
}

tsc -b checks both projects. A view that imports an intent file reads its declarations, which the intent project writes to node_modules/.tmp. The intent project has no reference to the app, so intents stay independent of the views.

Vite reads the same projects: with the configuration above, it compiles .intent.tsx files with Reintent's JSX and the rest with React's. So do the tools built on Vite, such as Vitest and React Router.

esbuild, tsx, Babel, SWC

These tools set the JSX from one configuration: esbuild and tsx from the tsconfig.json closest to each file, Babel and SWC from their own options. Start each .intent.tsx with this line:

reply.intent.tsx
/** @jsxImportSource @reintent/dom */

Every compiler reads this line, TypeScript included, so the line alone is enough, with a single tsconfig.json for the whole project.

Views read intents

An intent file's default export is its root. reintentView makes it a React component: each <Reply> owns an Intent, its props are the root's input, and what a person does goes back to that Intent. ReintentProvider gives the roots under it the model and the components their kinds are drawn with; a kind of the DOM catalog is drawn as its element.

src/reply.intent.tsx
import { action, choices, useChoose } from '@reintent/core';

export default function Reply(p: { to: string }) {
  const tone = useChoose(choices('formal', 'warm'), {
    of: 'reply',
    name: 'tone',
    defaultValue: 'warm',
  });
  return (
    <div>
      <p>
        A {tone} reply to {p.to}
      </p>
      <button onClick={action.decide({ of: 'reply', name: 'tone' }, 'formal')}>Formal</button>
    </div>
  );
}
src/App.tsx
import { ReintentProvider, reintentView } from '@reintent/react';
import ReplyProgram from './reply.intent';

const Reply = reintentView(ReplyProgram);

export function App() {
  return (
    <ReintentProvider>
      <Reply to="Ann" />
    </ReintentProvider>
  );
}

App renders A warm reply to Ann; after Formal, A formal reply to Ann.

Generated views

reintent-views writes the same line for every root, in a file of its own beside it, so React code imports a component whose props are the root's:

npx reintent-views src          # writes src/reply.intent.view.ts beside the root
npx reintent-views --check src  # in CI: fails when a view is missing or stale
src/App.tsx
import Reply from './reply.intent.view';

An Intent held outside React

An Intent made with createIntent, held by the app and worked on from outside React, is shown with <ReintentView intent>, and read and changed with useReintent(intent, select):

src/App.tsx
import { createIntent } from '@reintent/core';
import { ReintentView } from '@reintent/react';
import Reply from './reply.intent';

// Held by the app: undo, review a reprogramming, or show it in two places.
export const intent = createIntent({ program: Reply, input: { to: 'Ann' } });

export function App() {
  return <ReintentView intent={intent} />;
}

Verify your setup

Type errors

TypeScript catches JSX in the wrong file:

You wroteTypeScript says
<marquee /> in an .intent.tsx file, a kind outside the catalogProperty 'marquee' does not exist on type 'JSX.IntrinsicElements'.
A React component in an .intent.tsx file'ReplyView' cannot be used as a JSX component.
An intent component in a React file'Reply' cannot be used as a JSX component.

The build output

A compiled .intent.tsx file imports its JSX from @reintent/dom/jsx-runtime (or @reintent/core/jsx-runtime in a project with only intents), and a compiled view imports its JSX from react/jsx-runtime. When an intent file is compiled with React's JSX instead, createIntent throws:

Error: not an element: [object Object]

Check that the file ends in .intent.tsx, that tsconfig.intent.json includes it, and, with esbuild, tsx, Babel or SWC, that it starts with the @jsxImportSource line.

Next steps

Last updated on

Edit on GitHub