---
title: t
description: The type builders for key fields, columns, handler arguments, results and pushes, their modifiers, and how each value travels as JSON.
related:
  - /lunas/check-arguments-with-types
  - /sdk/table
  - /sdk/handler-spec
---

`t` holds the type builders that describe key fields, table columns, handler arguments and results, pushes, events and stream entries.

```ts luna/lunas/card.ts
import { defineLuna, t } from "luna";

export const Card = defineLuna({
  name: "Card",
  key: { board: t.string(), card: t.int() },
  handlers: (h) => ({
    move: h({ args: { to: t.enum(["todo", "doing", "done"]), note: t.string().optional() } }, (ctx, { to, note }) => {
      console.log(ctx.key.card, to, note ?? "");
    }),
  }),
});
```

## Reference

### Builders

```ts
import { t, type Infer } from "luna";
```

| Prop | Type | Default | Description |
| - | - | - | - |
| `t.string(opts?)` | `(opts?: { maxLength?: number }) => Ty<string>` | - | Text, at most maxLength characters when given. |
| `t.int()` | `() => Ty<number>` | - | A whole number. |
| `t.float()` | `() => Ty<number>` | - | Any number. |
| `t.bool()` | `() => Ty<boolean>` | - | true or false. |
| `t.timestamp()` | `() => Ty<number>` | - | Milliseconds since the Unix epoch, as a whole number. |
| `t.uuid()` | `() => Ty<string>` | - | A UUID, checked as one. |
| `t.bytes()` | `() => Ty<string>` | - | Bytes, as base64 text in arguments and results. |
| `t.json<V>()` | `<V = unknown>() => Ty<V>` | - | Any JSON value, typed as V in TypeScript and never checked. |
| `t.enum(values)` | `(values: readonly string[]) => Ty<one of values>` | - | One of the strings listed. |
| `t.array(items)` | `(items: Ty<I>) => Ty<I[]>` | - | A list whose items each match items. |
| `t.object(fields)` | `(fields: S) => Ty<InferObject<S>>` | - | An object with these fields and no others. |
| `t.id()` | `() => Ty<number>` | - | A column holding an integer primary key, given on insert. |
| `t.blobRef(bucket, opts?)` | `(bucket: string, opts?: { onDelete?: "restrict" \| "cascade" \| "setNull" }) => Ty<string>` | - | A column holding the path of a file in one of the luna's buckets, kept pointing at a file that exists. |
| `t.vector(dims, opts?)` | `(dims: number, opts?: { metric?: "cosine" \| "l2" \| "l1"; index?: boolean }) => Ty<Float32Array>` | - | A column holding dims float32 numbers, such as an embedding, searched with nearest(). |
| `t.request()` | `() => RequestArgs` | - | As a handler's args only: the handler takes the raw HTTP request. See Handler spec. |

### Modifiers

Each builder's result has these methods. Each returns a new type and leaves the original as it was.

| Prop | Type | Default | Description |
| - | - | - | - |
| `optional()` | `() => Ty<V \| null>` | - | The value may be null, or missing from an object or the arguments. |
| `default(value)` | `(value: V) => Ty<V>` | - | Columns only: the value a row gets when an insert leaves the column out. |
| `unique()` | `() => Ty<V>` | - | Columns only: no two rows may hold the same value. |
| `describe(text)` | `(text: string) => Ty<V>` | - | Says what a key field, argument or object field means. Kept with the app, and given no other meaning. |

### Options of t.vector

| Prop | Type | Default | Description |
| - | - | - | - |
| `dims` | `number` | - | How many numbers each vector holds, from 1 to 8192. |
| `metric?` | `"cosine" \| "l2" \| "l1"` | `"cosine"` | How nearest() measures distance. |
| `index?` | `boolean` | `false` | Keeps an approximate index for large tables. Below 10,000 rows, searches stay exact. |

### Options of t.blobRef

| Prop | Type | Default | Description |
| - | - | - | - |
| `bucket` | `string` | - | The bucket, by the name the luna type's blobs gives it. |
| `onDelete?` | `"restrict" \| "cascade" \| "setNull"` | `"restrict"` | Deleting the file fails, deletes the row, or sets the column to null. setNull needs an optional column. |

### Infer

`Infer<T>` is the TypeScript type of a builder's values.

```ts
const status = t.enum(["todo", "done"]);
type Status = Infer<typeof status>; // "todo" | "done"
```

## Good to know

Each builder fits some places and travels as a fixed JSON shape.

| Builder | In TypeScript | As JSON | Key field | Column |
| --- | --- | --- | --- | --- |
| `t.string()` | `string` | string | Yes | Yes |
| `t.int()` | `number` | integer | Yes | Yes |
| `t.float()` | `number` | number | No | Yes |
| `t.bool()` | `boolean` | `true` or `false` | No | Yes |
| `t.timestamp()` | `number` | integer | No | Yes |
| `t.uuid()` | `string` | string | Yes | Yes |
| `t.bytes()` | `string` | base64 string | Yes | Yes |
| `t.json()` | `V` | any | No | Yes |
| `t.enum()` | one of the values | string | Yes | Yes |
| `t.array()` | array | array | No | No |
| `t.object()` | object | object | No | No |
| `t.id()` | `number` | integer | No | Yes |
| `t.blobRef()` | `string` | string | No | Yes |
| `t.vector()` | `Float32Array` | array of numbers | No | Yes |

- A key field is required. An optional builder cannot be one.
- Arrays and objects cannot be columns. Store them in a `t.json()` column.
- A `t.bytes()` column takes base64 text or a `Uint8Array`, and reads back as base64 text, as arguments carry it. A `t.vector()` column takes a `Float32Array` or an array of numbers and reads back as a `Float32Array`.
- An argument or result of `t.int()` or `t.timestamp()` with a fraction fails the check. `Date.now()` is a whole number.
- `maxLength` counts characters, not bytes.
- `t.id()` adds its column to the front of the table's primary key. A vector column cannot be `unique()` or have a `default()`.

## Examples

### Reuse an object type

Declare the type once and use it in arguments, columns and TypeScript.

```ts luna/lunas/card.ts
import { defineLuna, t, table, type Infer } from "luna";

const label = t.object({ name: t.string({ maxLength: 40 }), color: t.enum(["red", "green", "blue"]) });
export type Label = Infer<typeof label>;

export const Card = defineLuna({
  name: "Card",
  key: { board: t.string(), card: t.int() },
  schema: { card: table({ title: t.string(), labels: t.json<Label[]>().default([]) }, { singleRow: true }) },
  handlers: (h) => ({
    edit: h({ args: { title: t.string(), labels: t.array(label) } }, (ctx, { title, labels }) => {
      ctx.db.card.set({ title, labels });
    }),
  }),
});
```

### Say what a field means

```ts luna/lunas/card.ts
key: { board: t.string().describe("The board the card is on"), card: t.int() },
```

### Accept an argument that may be left out

```ts luna/lunas/card.ts
due: h({ args: { at: t.timestamp().optional() } }, (ctx, { at }) => {
  console.log(at ?? "no due date");
}),
```
