Skip to content

Commit

Permalink
`types(groq-builder): ExpandAliasValues
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Oct 5, 2023
1 parent c54539a commit 30f1e8f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/nextjs/sanity-studio/builder/groq-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Parser } from "./common-types";

import "./commands";
import { MaybeArrayItem, SimplifyDeep } from "./type-utils";
import { RootConfig } from "./schema-types";
import { ExpandAliasValuesDeep, RootConfig } from "./schema-types";

type RootScope = never;

export function createGroqBuilder<TRootConfig extends RootConfig>() {
return new GroqBuilder<RootScope, TRootConfig>("", null);
return new GroqBuilder<RootScope, ExpandAliasValuesDeep<TRootConfig, TRootConfig>>("", null);
}

export class GroqBuilder<TScope, TRootConfig extends RootConfig> {
Expand Down
8 changes: 7 additions & 1 deletion packages/nextjs/sanity-studio/builder/schema-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AliasValue } from "@sanity-typed/types";
import { Get, Primitive, TypeMismatchError, ValueOf } from "./type-utils";
import { InferRawValue, DefinitionBase } from "@sanity-typed/types";
import { Get, Primitive, Simplify, TypeMismatchError, ValueOf } from "./type-utils";

export type RootConfig = { TSchema: any; referenced: symbol };
export type RefType<referencedSymbol extends symbol, TTypeName> = {
Expand All @@ -15,6 +16,7 @@ export type ExtractRefType<TScope, TRootConfig extends RootConfig> =
actual: TScope;
}>;
export type ExtractDocumentTypes<TRootConfig extends RootConfig> = Array<ValueOf<TRootConfig["TSchema"]>>;

export type ExpandAliasValuesDeep<T, TRootConfig extends RootConfig> = T extends AliasValue<infer TDocType>
? Get<TRootConfig["TSchema"], TDocType>
: T extends Primitive
Expand All @@ -24,3 +26,7 @@ export type ExpandAliasValuesDeep<T, TRootConfig extends RootConfig> = T extends
: {
[P in keyof T]: ExpandAliasValuesDeep<T[P], TRootConfig>;
};

export type InferSchemaValuesFromDocuments<TDocumentTypes extends Record<string, DefinitionBase<any, any, any>>> = {
[P in keyof TDocumentTypes]: Simplify<{ _type: P } & Omit<InferRawValue<TDocumentTypes[P]>, "_type">>;
};
9 changes: 3 additions & 6 deletions packages/nextjs/sanity-studio/sanity-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { _referenced, aliasedType, InferRawValue, DefinitionBase } from "@sanity-typed/types";
import { _referenced, aliasedType } from "@sanity-typed/types";
import { DocumentTypes } from "./schemas/schema";
import { Simplify } from "./builder/type-utils";
import { InferSchemaValuesFromDocuments } from "./builder/schema-types";
// import { InferSchemaValues } from "@sanity-typed/types";

// import sanityConfig from "./sanity.config";
// export type SanitySchema = InferSchemaValues<typeof sanityConfig>;
Expand All @@ -13,10 +14,6 @@ import { Simplify } from "./builder/type-utils";
*/
export type SanitySchema = InferSchemaValuesFromDocuments<DocumentTypes>;

type InferSchemaValuesFromDocuments<TDocumentTypes extends Record<string, DefinitionBase<any, any, any>>> = {
[P in keyof TDocumentTypes]: Simplify<{ _type: P } & Omit<InferRawValue<TDocumentTypes[P]>, "_type">>;
};

export type SchemaConfig = {
TSchema: SanitySchema;
referenced: typeof _referenced;
Expand Down

0 comments on commit 30f1e8f

Please sign in to comment.