# TypeScript or Rust?

> Both SDKs build the same kind of bot. How they differ in size, speed and the few TypeScript-specific rules.

`@zaun/bot` (TypeScript) and the `zaun-bot` crate (Rust) are two SDKs for the same thing: a
WebAssembly component the host runs in its sandbox, with the same commands, components, modals,
events, direct messages, voice, storage, timers and HTTP. The manifest, `zbot pack`, signing and
installing are identical. These docs show TypeScript first.

| | TypeScript (`@zaun/bot`) | Rust (`zaun-bot`) |
|---|---|---|
| API style | builder classes and interaction objects (`SlashCommandBuilder`, `interaction.reply`) | a `Bot` trait with handler methods, builders |
| Build | `zbot build`: esbuild bundles `src/index.ts`, jco makes the component | `zbot build`: `cargo build --target wasm32-wasip2` |
| Package size | about **24 MB** (the JavaScript engine is inside) | about **300 KB** |
| First load on a host | about **11 s** to compile, then cached | well under a second |
| A warm call | tens of microseconds (about 22 µs) | under a microsecond (about 0.4 µs) |
| Errors | thrown `ZaunError { code, detail, retryAfterMs }` | `Result<_, HostError>` |

Either is fast enough for chat: a call is a tiny part of what a member waits for. Choose
TypeScript if you know JavaScript, Rust if you want the smallest, fastest package.

## Rules that are specific to TypeScript

- **No `setTimeout` or `setInterval`.** Nothing runs between calls, so they never fire. Use
  `bot.timers.after(ms, tag)` or `bot.timers.every(ms, tag)` and listen with `bot.on('timer', …)`.
- **Autocomplete answers synchronously.** Call `interaction.respond(choices)` before the
  handler's first `await`: the choices are taken when the handler returns.
- **Async is fine otherwise.** Host calls are synchronous, so awaiting them (or promises that are
  already resolved) completes inside the same call.
- **No `fetch`.** Use `bot.http.fetch(url, options)`, which goes only to hosts in the bot's
  `network` grant.
- **The entry exports the world.** End `src/index.ts` with
  `export const { init, onEvent, onInteraction, onAutocomplete, onTimer, onVoiceEvent } = bot.exports();`
- Builders check the limits (custom ids ≤ 100 bytes, ≤ 5 buttons per row, modal titles ≤ 45, …)
  and throw `ZaunValidationError` before anything reaches the host.

## Reference

- [`@zaun/bot` API](/docs/reference/js/zaun-bot/readme/) (generated from the TypeScript source)
- [`zaun-bot` crate](/docs/reference/rust/zaun-bot/) (generated from rustdoc)
- [Bot WIT interface](/docs/reference/wit/), which both SDKs wrap
