Typed reads
Mapped records typed from the widget's declaration and checked cell by cell, with read-only access.
A widget declares the columns it needs, with their Grist types, in
defineGristWidget({ columns }). That one declaration gives it typed
records, checked at runtime, with no schema to fetch and no full access.
Declare once
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
title: "Tasks",
requiredAccess: "read table",
columns: [
{ name: "Title", type: "Text" },
{ name: "Due", type: "Date", optional: true },
{ name: "Owner", type: "Ref:People" },
],
})
function Tasks() {
const w = widget.useGrist()
// w.mappedRecords: { id: number; Title: string | null; Owner: number | null; Due?: Date | null }[] | null
return (
<ul>
{(w.mappedRecords ?? []).map((t) => (
<li key={t.id}>{t.Title}{t.Due ? ` — ${t.Due.toLocaleDateString(undefined, { timeZone: "UTC" })}` : ""}</li>
))}
</ul>
)
}defineGristWidget() keeps the column names and types literal, so
widget.useGrist() is typed from the declaration: no generics, no
annotation. GristWidgetRow<typeof widget> names the record type for a
component's props. src/app/App.tsx exports widget, and mountGristWidget()
reads it (with the title, for the page).
What a cell becomes
Grist's mapping panel only offers the columns of the declared type, and the plugin API's values already say what they are, so each mapped cell is turned into the value its type promises:
| Declared type | Value | Empty cell |
|---|---|---|
Text | string | "" |
Numeric, Int | number | null |
Bool | boolean | false |
Date, DateTime | Date | null |
Choice | string | null |
ChoiceList | string[] | [] |
Ref | the row id, number | null |
RefList, Attachments | row ids, number[] | [] |
Any, or no type | as Grist sends it | — |
A Date cell is its day at midnight UTC: show it in UTC
(toLocaleDateString(undefined, { timeZone: "UTC" })), or a reader west of
UTC sees the day before. A DateTime is an instant: show it in the
person's time zone, or the column's.
A declared Ref or RefList column reads as row ids, whatever its column
shows. Grist sends a reference as the text its column shows (a person's
name, or a number that isn't the row id), so a widget that declares one asks
Grist for the ids instead: w.record and w.records then hold row ids for
every reference, not only the declared one, and an empty reference is 0
there (null in the mapped records); w.fetchRows() gives the same ids. To
read the name, declare the column "Text,Ref", and no column a pure Ref:
Grist sends either the ids or the names, for every column at once, so next to
a Ref column a "Text,Ref" one reads the id too (the SDK warns about it in
the console). To show a referenced row's name next to its id, read its table
with w.fetchRows(tableId) (full access).
A column that accepts several types ("Text,Ref") takes the first that
matches. An allowMultiple column is a list, one value per mapped column.
Grist offers a column for mapping by its base type: a declared Ref:People
accepts any reference column, DateTime:Europe/Paris any date and time
column. Neither Grist nor the SDK checks what follows the colon: a widget
that follows the ids into a table should check the mapped column's type
first (w.listColumns()).
Cells that don't match
A cell that doesn't match its declared type is null in mappedRecord /
mappedRecords, and listed in w.cellIssues. That is why the record type
gives every declared column its value or null, Text and Bool
included:
type GristCellIssue = { rowId: number; column: string; message: string; value: unknown }
// { rowId: 4, column: "Amount", message: "not a number", value: "n/a" }This happens when a Numeric cell holds text Grist couldn't parse (Grist
keeps the text), a formula fails (formula error (ZeroDivisionError)), or an
Any column holds a value of another type. (A column whose type changes to
one the declaration doesn't accept is unmapped instead.) mountGristWidget() shows an
alert above the widget naming the first one (cell-issues).
More rules: a Standard Schema
For rules beyond the type (a range, an email format), pass any
Standard Schema validator as schema in the
declaration: zod 4, valibot and arktype all implement it. It runs on each
mapped record after the type checks; its issues join cellIssues, and its
output (coercions, defaults) becomes the record. The SDK depends on none of
these libraries: a widget ships one only if it uses it.
import { z } from "zod"
import { defineGristWidget } from "grist-widget-sdk"
export const widget = defineGristWidget({
requiredAccess: "read table",
columns: [{ name: "Email", type: "Text" }, { name: "Age", type: "Int" }],
schema: z.object({ Email: z.string().email(), Age: z.number().min(0).nullable() }).passthrough(),
})Only synchronous validators: an asynchronous one is reported as an issue.
Any table
checkGristCell(type, value) checks one value against a Grist type, for data
read some other way (fetchRows(tableId), with the types from listColumns):
import { checkGristCell } from "grist-widget-sdk"
const check = checkGristCell("Numeric", row.Amount)
if (!check.ok) console.warn(check.message)Without columns, fetchRows(tableId) gives Grist's stored form, where a
list starts with "L": pass { encoded: true }. The section's rows, and a
fetchRows(tableId, { columns }), are decoded: pass { encoded: false },
so a choice "L" stays a choice.
The raw rows
w.record and w.records hold the same rows under the table's own column
ids, decoded but unchecked: Grist doesn't type them, so neither does the
SDK (id is always a number, the rest unknown). Prefer the mapped
records, which are checked.