Inferred types
Infer<typeof schema> gives you the document type, including _key on array items and the system fields Sanity adds.
Declare a content model once. Get its TypeScript types and a runtime validator from the same definition.
import * as s from '@sanity-labs/typed-schema'
export const Recipe = s.document({
_type: s.literal('recipe'),
title: s.string(),
servings: s.optional(s.number()),
})
export type Recipe = s.Infer<typeof Recipe>
const result = s.parse(Recipe, await client.fetch('*[_type == "recipe"][0]'))
if (result.ok) {
const recipe = result.value} else {
// for a recipe without a title and with "four" as servings:
// Invalid input (2 issues):
// title: Expected a string
// servings: Expected a finite number
console.error(result.error.message)
}