ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas

Home Page:sanity-codegen-dev.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sanity Codegen ✨

Generate TypeScript types from your Sanity schemas and queries

Installation

# NOTE: the alpha is required at this time
npm i --save-dev sanity-codegen@alpha

CLI Usage

At the root of your sanity project, run:

npx sanity-codegen codegen

This command will locate your schema, generate TypeScript types, and write them to sanity-codegen.d.ts.

You can also create a configuration file and see other options here.

Type usage

Schema types

The sanity-codegen.d.ts file that was generated will add ambient types to your project. Access them via Sanity.{WorkspaceName}.Schema.{TypeName}

interface Props {
  book: Sanity.Default.Schema.Book; // no import needed. just use it
}

function yourFunction({ book }: Props) {
  //
}

Query types (aka GROQ-codegen)

See usage with the @sanity-codegen/client.

About

Generate TypeScript types from your Sanity.io schemas

sanity-codegen-dev.vercel.app

License:MIT License


Languages

Language:TypeScript 97.1%Language:JavaScript 1.8%Language:HTML 0.5%Language:Shell 0.5%Language:CSS 0.1%Language:Batchfile 0.0%