> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript

> Types come from the declarations: value types, facts, keys and a client typed from the app tree.

Every type in a Bijection app comes from a declaration. A function's argument
types come from its `args`, its value from `returns` or the handler, and its
refusal codes from `refuses`. A table carries its fact and key types, and the
app tree types the client.

```ts bijection/messages.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, role, t, table } from "@bijectionhq/bijection";

export const messages = table({ body: t.string, author: t.principal, sent: t.instant }, { index: [["sent"]] });

export const send = mutation(
  { allow: [role.signedIn], args: { body: t.string }, refuses: { Empty: null } },
  async (ctx, { body }) => {
    if (body.trim() === "") ctx.refuse("Empty", "Write a message first.");
    return await ctx.insert(messages, { body, author: ctx.frame.as.id, sent: ctx.frame.now.entry });
  },
);
```

## Reuse inferred types

| Type | From | Is |
| - | - | - |
| `FactOf<typeof messages>` | `@bijectionhq/bijection` | A fact of the table. |
| `KeyOf<typeof messages>` | `@bijectionhq/bijection` | Its key. |
| `ValueOf<typeof shape>` | `@bijectionhq/bijection` | The value of a value type. |
| `ArgsOf<typeof send>`, `ReturnsOf<typeof send>`, `RefusesOf<typeof send>` | `@bijectionhq/bijection` | A declaration or client reference's arguments, return value and map of refusal codes to payload types. |
| `Result<V, C>` | `@bijectionhq/bijection` | How a call ended: `{ ok: true, value }` or `{ ok: false, code, message, guard? }`. |

Inside a declaration, the value types are `t.key(messages)` and
`t.fact(messages)`. Durations and instants are `Temporal` values, exact
decimals are `Decimal`, and 64-bit integers (`t.int`) are `bigint`.

For a client, import the app as a type: `api<typeof App>()`. See
[TypeScript clients](https://docs.bijection.com/clients/typescript.md).

## Frontend declarations

The React starters reference the backend through `tsconfig.bijection.json`.
Their type imports still point to `bijection/app.ts`. TypeScript resolves
those imports to the cached declarations in `.bijection/type-cache/`.
Declaration maps let editors navigate back to the source. Keep the
cache directory out of version control.

`bijection dev` watches the declaration project using the workspace's
installed TypeScript compiler. The starter's check and build commands
populate the cache on a fresh checkout. A backend edit that leaves its
exported types unchanged lets TypeScript reuse the frontend's prior work.
The CLI's pinned native compiler still checks the backend for publication.

## What the types check, and what `check` checks

The SDK's types check shapes: that a selection has an index to serve it, that
a verb takes the address you pass, that a spec key exists, and that a value
fits its type. A misuse reads as a remedy sentence at the call site. Label flow,
such as reading above a function's `under`, is checked by
[`bijection check`](https://docs.bijection.com/development/cli/check) and enforced by the host, never by
the compiler.

## Type-check

`bijection dev`, `bijection deploy` and `bijection check` type-check the
backend with the CLI's pinned native TypeScript compiler. You need neither
Node.js nor a project-installed compiler for the backend, and changing your
project's TypeScript dependency does not change the one the CLI uses. Run the
same check alone with:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection typecheck
```

Each check is bounded to two minutes and reports the compiler's diagnostics
on failure. If type-checking is slow, a single explicit annotation often
breaks a long inference chain. Find where the compiler spends its time with
[`generateTrace`](https://github.com/microsoft/TypeScript-wiki/blob/main/Performance-Tracing.md):

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
npx tsc -p bijection --generateTrace trace --incremental false
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.