Skip to content

Commit

Permalink
feature!: replaced Schema namespace with plain exports for schema-def…
Browse files Browse the repository at this point in the history
…initions (#3699)

* feature!: replaced Schema namespace with plain exports for schema-definitions

This fixes naming conflict between namespace and interface, and will make definition type extensions
easier to work with (by declaration merging interfaces in sanity module directly).

Option-definitions is now shared between SchemaTypes and Definition types.
Some base types for definitions has been renamed.

BREAKING CHANGE: Code that accessed types in the Schema namespace must import the types directly instead.
  • Loading branch information
snorrees committed Oct 4, 2022
1 parent 841121a commit 05068d8
Show file tree
Hide file tree
Showing 62 changed files with 990 additions and 809 deletions.
8 changes: 4 additions & 4 deletions dev/test-studio/structure/groupByOption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StructureBuilder} from 'sanity/desk'
import {Schema} from '@sanity/types'
import {ObjectOptions, Schema} from '@sanity/types'

type StructureGroup = 'v3' // extend with union strings

Expand All @@ -23,8 +23,8 @@ export function typesInOptionGroup(
})
}

export function structureGroupOptions<
O extends Required<StructureGroupOption> & Schema.ObjectOptions
>(options: O): O & Schema.ObjectOptions {
export function structureGroupOptions<O extends Required<StructureGroupOption> & ObjectOptions>(
options: O
): O & ObjectOptions {
return options
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as assert from 'assert'
import type {ObjectSchemaType} from '@sanity/types'
import blockContentTypeFeatures from '../../../src/util/blockContentTypeFeatures'
import customSchema from '../../fixtures/customSchema'
Expand Down
16 changes: 12 additions & 4 deletions packages/@sanity/types/src/reference/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ export type ReferenceFilterResolver = (options: {
parentPath: Path
}) => ReferenceFilterSearchOptions | Promise<ReferenceFilterSearchOptions>

export type ReferenceFilterOptions =
| {filter: ReferenceFilterResolver}
| {filter: string; filterParams?: Record<string, unknown>}
export interface ReferenceFilterResolverOptions {
filter: ReferenceFilterResolver
}
export interface ReferenceFilterQueryOptions {
filter: string
filterParams?: Record<string, unknown>
}

export interface ReferenceBaseOptions {
disableNew?: boolean
}

export type ReferenceOptions = {disableNew?: boolean} & ReferenceFilterOptions
export type ReferenceFilterOptions = ReferenceFilterResolverOptions | ReferenceFilterQueryOptions
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/asserters.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {CrossDatasetReferenceSchemaType} from '../crossDatasetReference'
import {TitledListValue} from './definition'
import type {
BlockSchemaType,
ArraySchemaType,
ObjectSchemaType,
ReferenceSchemaType,
SpanSchemaType,
TitledListValue,
BlockChildrenObjectField,
StyleObjectField,
ListObjectField,
Expand Down
50 changes: 23 additions & 27 deletions packages/@sanity/types/src/schema/define.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {Schema} from './types'
import type {
DefineArrayMemberBase,
DefineOptions,
DefineSchemaOptions,
DefineSchemaBase,
MaybeAllowUnknownProps,
NarrowPreview,
StrictDefinition,
WidenInitialValue,
WidenValidation,
} from './defineTypes'
import {FieldDefinitionBase, IntrinsicTypeName} from './definition'

/**
* Helper function for defining a Sanity type definition. This function does not do anything on its own;
Expand All @@ -23,7 +23,7 @@ import type {
* If you know the base type of the type-alias, provide `defineOptions.aliasFor: <base type name>`.
* This will enforce that the schema definition conforms with the provided type.
*
* By default `defineType` only allows known properties and options.
* By default, `defineType` only allows known properties and options.
* Use `defineOptions.strict: false` to allow unknown properties and options.
*
* ### Basic usage
Expand Down Expand Up @@ -109,15 +109,10 @@ import type {
*
* //redeclare the sanity module
* declare module 'sanity' {
*
* // redeclare Schema; it will be merged with Schema in the sanity module
* export namespace Schema {
*
* // redeclare StringOptions; it will be merged with Schema.StringOptions in the sanity module
* export interface StringOptions {
* myCustomOption?: boolean
* }
* }
* // redeclare StringOptions; it will be merged with StringOptions in the sanity module
* export interface StringOptions {
* myCustomOption?: boolean
* }
* }
*
* // the option is now part of the StringOptions type, just as if it was declared in the sanity codebase:
Expand All @@ -144,10 +139,11 @@ import type {
* }
* }
*
* // redeclares sanity module so we can add interfaces props to it
* declare module 'sanity' {
* // redeclares IntrinsicTypeDefinition and adds a named definition to it
* // it is very important that the key is the same as the type in the definition ('magically-added-type')
* export interface IntrinsicTypeDefinition {
* // redeclares IntrinsicDefinitions and adds a named definition to it
* // it is important that the key is the same as the type in the definition ('magically-added-type')
* export interface IntrinsicDefinitions {
* 'magically-added-type': MagicallyAddedDefinition
* }
* }
Expand All @@ -172,11 +168,11 @@ import type {
* @see typed
*/
export function defineType<
TType extends string | Schema.Type, // Schema.Type here improves autocompletion in _some_ IDEs (not VS Code atm)
TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm)
TName extends string,
TSelect extends Record<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
TAlias extends Schema.Type | undefined,
TAlias extends IntrinsicTypeName | undefined,
TStrict extends StrictDefinition
>(
schemaDefinition: {
Expand All @@ -187,7 +183,7 @@ export function defineType<
MaybeAllowUnknownProps<TStrict>,

// eslint-disable-next-line @typescript-eslint/no-unused-vars
defineOptions?: DefineOptions<TStrict, TAlias>
defineOptions?: DefineSchemaOptions<TStrict, TAlias>
): typeof schemaDefinition {
return schemaDefinition
}
Expand All @@ -211,11 +207,11 @@ export function defineType<
* @see typed
*/
export function defineField<
TType extends string | Schema.Type, // Schema.Type here improves autocompletion in _some_ IDEs (not VS Code atm)
TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm)
TName extends string,
TSelect extends Record<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
TAlias extends Schema.Type | undefined,
TAlias extends IntrinsicTypeName | undefined,
TStrict extends StrictDefinition
>(
schemaField: {
Expand All @@ -224,10 +220,10 @@ export function defineField<
} & DefineSchemaBase<TType, TAlias> &
NarrowPreview<TType, TAlias, TSelect, TPrepareValue> &
MaybeAllowUnknownProps<TStrict> &
Schema.FieldBase,
FieldDefinitionBase,

// eslint-disable-next-line @typescript-eslint/no-unused-vars
defineOptions?: DefineOptions<TStrict, TAlias>
defineOptions?: DefineSchemaOptions<TStrict, TAlias>
): typeof schemaField & WidenValidation & WidenInitialValue {
return schemaField
}
Expand All @@ -251,11 +247,11 @@ export function defineField<
* @see typed
*/
export function defineArrayMember<
TType extends string | Schema.Type, // Schema.Type here improves autocompletion in _some_ IDEs (not VS Code atm)
TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm)
TName extends string,
TSelect extends Record<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
TAlias extends Schema.Type | undefined,
TAlias extends IntrinsicTypeName | undefined,
TStrict extends StrictDefinition
>(
arrayOfSchema: {
Expand All @@ -272,7 +268,7 @@ export function defineArrayMember<
MaybeAllowUnknownProps<TStrict>,

// eslint-disable-next-line @typescript-eslint/no-unused-vars
defineOptions?: DefineOptions<TStrict, TAlias>
defineOptions?: DefineSchemaOptions<TStrict, TAlias>
): typeof arrayOfSchema & WidenValidation & WidenInitialValue {
return arrayOfSchema
}
Expand All @@ -288,11 +284,11 @@ export function defineArrayMember<
* defineField({
* type: 'string',
* name: 'nestedField',
* options: typed<Schema.StringOptions & {myCustomOption: boolean}>({
* options: typed<StringOptions & {myCustomOption: boolean}>({
* layout: 'radio',
* // allowed
* myCustomOption: true,
* //@ts-expect-error unknownProp is not part of AssetFieldOptions & Schema.StringOptions
* //@ts-expect-error unknownProp is not part of StringOptions & {myCustomOption: boolean}
* unknownProp: 'not allowed in typed context',
* }),
* }),
Expand Down
41 changes: 22 additions & 19 deletions packages/@sanity/types/src/schema/defineTypes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {
ArrayOfEntry,
IntrinsicDefinitions,
TypeAliasDefinition,
IntrinsicTypeName,
} from './definition'
import type {PreviewConfig} from './preview'
import type {InitialValueProperty, Schema, SchemaValidationValue} from './types'
import type {InitialValueProperty, SchemaValidationValue} from './types'

export interface DefineOptions<
export interface DefineSchemaOptions<
TStrict extends StrictDefinition,
TAlias extends Schema.Type | undefined
TAlias extends IntrinsicTypeName | undefined
> {
/**
* `strict: false` allows unknown properties in the schema.
Expand All @@ -18,38 +24,35 @@ export interface DefineOptions<
*/
strict?: TStrict
/** Should be provided when type is a non-intrinsic type, ie type is a type alias */
aliasFor?: TAlias extends Schema.Type ? TAlias : never
aliasFor?: TAlias extends IntrinsicTypeName ? TAlias : never
}

export type IntrinsicBase = {
[K in keyof Schema.IntrinsicTypeDefinition]: Omit<Schema.IntrinsicTypeDefinition[K], 'preview'>
[K in keyof IntrinsicDefinitions]: Omit<IntrinsicDefinitions[K], 'preview'>
}

export type IntrinsicArrayOfBase = {
[K in keyof Schema.IntrinsicTypeDefinition]: Omit<
Schema.ArrayOfEntry<Schema.IntrinsicTypeDefinition[K]>,
'preview'
>
[K in keyof IntrinsicDefinitions]: Omit<ArrayOfEntry<IntrinsicDefinitions[K]>, 'preview'>
}

export type DefineSchemaBase<
TType extends string,
TAlias extends Schema.Type | undefined
> = TType extends Schema.Type ? IntrinsicBase[TType] : Schema.TypeAliasDefinition<TType, TAlias>
TAlias extends IntrinsicTypeName | undefined
> = TType extends IntrinsicTypeName ? IntrinsicBase[TType] : TypeAliasDefinition<TType, TAlias>

export type DefineSchemaType<
TType extends string,
TAlias extends Schema.Type | undefined
> = TType extends Schema.Type
? Schema.IntrinsicTypeDefinition[TType]
: Schema.TypeAliasDefinition<TType, TAlias>
TAlias extends IntrinsicTypeName | undefined
> = TType extends IntrinsicTypeName
? IntrinsicDefinitions[TType]
: TypeAliasDefinition<TType, TAlias>

export type DefineArrayMemberBase<
TType extends string,
TAlias extends Schema.Type | undefined
> = TType extends Schema.Type
TAlias extends IntrinsicTypeName | undefined
> = TType extends IntrinsicTypeName
? IntrinsicArrayOfBase[TType]
: Schema.ArrayOfEntry<Schema.TypeAliasDefinition<string, TAlias>>
: ArrayOfEntry<TypeAliasDefinition<string, TAlias>>

export type StrictDefinition = boolean | undefined

Expand All @@ -71,7 +74,7 @@ type MaybePreview<

export type NarrowPreview<
TType extends string,
TAlias extends Schema.Type | undefined,
TAlias extends IntrinsicTypeName | undefined,
TSelect extends Record<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined
> = DefineSchemaType<TType, TAlias> extends {preview?: Record<string, any>}
Expand Down
2 changes: 2 additions & 0 deletions packages/@sanity/types/src/schema/definition/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './type'
export * from './schemaDefinition'

0 comments on commit 05068d8

Please sign in to comment.