TypeScript
Connect a TypeScript data source to rightui components.
Prepare your app
Complete Installation, including the font setup. The following two files form a working example. Copy their contents into app.contract and app.ts in your Exact app.
Declare the UI in app.contract
Button comes from @rightui/ui. The greeting resource requests a record with the shape declared above the component.
contract
use defaultFontWeights from "@rightui/ui"
font "Geist"
100 900 = "assets/fonts/Geist.ttf"
100 900 italic = "assets/fonts/Geist-Italic.ttf"
font "Newsreader"
200 800 = "assets/fonts/Newsreader.ttf"
200 800 italic = "assets/fonts/Newsreader-Italic.ttf"
use uiFontFamily, Button from "@rightui/ui"
shape Greeting
label: string
component App
provide
uiFont = "theme"
uiWeights = defaultFontWeights()
resource greeting = greeting() as shape Greeting
state count = 0
action increment
count = count + 1
view
column padding=24 gap=12 color="ui.foreground" background-color="ui.background"
text greeting.label font-family=uiFontFamily("theme") data-rightui-font="body"
text count.toString() testId="counter" font-family=uiFontFamily("theme") data-rightui-font="body"
Button(id="increment", label="Increment", variant="primary", disabled=false, press=increment)
Provide the data in app.ts
The answer function returns the data for greeting. Exact provides the generated Answer type after the next step.
typescript
import type { Answer } from './app.contract.d.ts';
export const grants = '';
export const answer: Answer = (source) => {
if (source === 'greeting') return { label: 'Hello from TypeScript' };
throw new Error(`Unknown source: ${source}`);
};
Generate types and run
Run these commands from your app directory. The page displays the greeting and an interactive counter.
sh
bun exact.mjs contract types app.contract -o app.contract.d.ts
bun exact.mjs web