Getting started
Requires TypeScript 6.0 or later with strict: true in tsconfig.json.
sh
npm install @sanity-labs/typed-schemash
pnpm add @sanity-labs/typed-schemash
yarn add @sanity-labs/typed-schemash
bun add @sanity-labs/typed-schemaThe package is published as ES modules only.
A first schema
The examples on this site import the package as a namespace, import * as s from '@sanity-labs/typed-schema', so creators read as s.document(), s.string() and so on. Named imports work just as well.
Every schema is a value built from creator functions. Infer turns a schema into its TypeScript type, and parse checks a value against the schema at runtime:
ts
import * as s from '@sanity-labs/typed-schema'
const Recipe = s.document({
_type: s.literal('recipe'),
title: s.string(),
servings: s.optional(s.number()),
})
type Recipe = s.Infer<typeof Recipe>
const response = await fetch('https://example.com/recipes/pancakes.json')
const result = s.parse(Recipe, await response.json())
if (result.ok) {
const recipe = result.value}Hover any name in the examples on this site to see its type. To try your own schemas, open the playground.
Next steps
- Declaring a document covers the creators you will use most.
- Parsing and errors explains runtime validation.
- Why typed schemas describes what the library checks that a classic schema does not.