Package Exports
- @sanity-typed/client
- @sanity-typed/client/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sanity-typed/client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@sanity-typed/client
Typed Sanity Client Results, all inferred, no client changes!
Page Contents
Install
npm install @sanity-typed/client
Usage
Use createClient
exactly as you would from @sanity/client
with a minor change for proper type inference.
your-super-cool-application.ts
:
// import { createClient } from "@sanity/client";
import { createClient } from "@sanity-typed/client";
// Get this from @sanity-typed/types!
import type { SanityValues } from "./sanity.schema";
/** Small change using createClient */
// const client = createClient({
const client = createClient<SanityValues>()({
projectId: "your-project-id",
dataset: "your-dataset-name",
useCdn: true,
apiVersion: "2023-05-23",
});
/** Typescript type from GROQ queries! */
const data = await client.fetch('*[_type=="product"]');
/**
* typeof data === {
* _createdAt: string;
* _id: string;
* _rev: string;
* _type: "product";
* _updatedAt: string;
* productName?: string;
* tags?: {
* _key: string;
* label?: string;
* value?: string;
* }[];
* }[]
*/
The createClient<SanityValues>()(config)
syntax is due to having to infer one generic (the config shape) while explicitly providing the Sanity Values' type, which can't be done in the same generics.