Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Named reference types #192

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ReferenceType = {
// blog example doesn't follow this.
to: { type: string } | Array<{ type: string }>;
weak?: boolean;
name?: string;
};
type SlugType = { name?: string; type: 'slug' };
type StringType = {
Expand Down Expand Up @@ -295,6 +296,15 @@ async function generateTypes({
)
.join(' | ');

const name = (obj as ReferenceType)?.name;

// In the case that a new, named schema type is defined as a reference.
// Sanity treats this like a normal reference, but sets `_type` like
// an object. based on `name`.
if (!parents.length && name) {
return `{_type: '${name}'; _ref: string}`;
}

// Note: we want the union to be wrapped by one Reference<T> so when
// unwrapped the union can be further discriminated using the `_type`
// of each individual reference type
Expand Down