From ebe4e070a1161c1be4b01c1ae148223bbd1ebb3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Gabri=C4=8D?= Date: Fri, 19 Apr 2024 12:13:00 +0200 Subject: [PATCH 1/2] feat(core): improve defineType TType --- packages/@sanity/types/src/schema/define.ts | 3 ++- packages/@sanity/types/src/schema/types.ts | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/@sanity/types/src/schema/define.ts b/packages/@sanity/types/src/schema/define.ts index 247a9c1174f..84e51f48afe 100644 --- a/packages/@sanity/types/src/schema/define.ts +++ b/packages/@sanity/types/src/schema/define.ts @@ -9,6 +9,7 @@ import { type WidenValidation, } from './defineTypes' import {type FieldDefinitionBase, type IntrinsicTypeName} from './definition' +import {type AllowOtherStrings} from './types' /** * Helper function for defining a Sanity type definition. This function does not do anything on its own; @@ -170,7 +171,7 @@ import {type FieldDefinitionBase, type IntrinsicTypeName} from './definition' * @beta */ export function defineType< - const TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm) + const TType extends IntrinsicTypeName | AllowOtherStrings, const TName extends string, TSelect extends Record | undefined, TPrepareValue extends Record | undefined, diff --git a/packages/@sanity/types/src/schema/types.ts b/packages/@sanity/types/src/schema/types.ts index 7db84e38b0c..adcaadd7858 100644 --- a/packages/@sanity/types/src/schema/types.ts +++ b/packages/@sanity/types/src/schema/types.ts @@ -24,6 +24,10 @@ import {type PreviewConfig} from './preview' export {defineArrayMember, defineField, defineType, typed} from './define' +// Necessary since this is the only way to include all string literals and all other strings +// eslint-disable-next-line @typescript-eslint/ban-types +export type AllowOtherStrings = string & {} + /** * Note: you probably want `SchemaTypeDefinition` instead * @see SchemaTypeDefinition From 4205ef725a663781d83ca806bbac2dbd75270c0f Mon Sep 17 00:00:00 2001 From: Rico Kahler Date: Mon, 22 Apr 2024 15:51:28 -0500 Subject: [PATCH 2/2] fix: add tsdoc comment; add to `defineField` and `defineArrayMember` --- packages/@sanity/types/src/schema/define.ts | 8 ++++---- packages/@sanity/types/src/schema/types.ts | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/@sanity/types/src/schema/define.ts b/packages/@sanity/types/src/schema/define.ts index 84e51f48afe..6abe047c151 100644 --- a/packages/@sanity/types/src/schema/define.ts +++ b/packages/@sanity/types/src/schema/define.ts @@ -9,7 +9,7 @@ import { type WidenValidation, } from './defineTypes' import {type FieldDefinitionBase, type IntrinsicTypeName} from './definition' -import {type AllowOtherStrings} from './types' +import {type AutocompleteString} from './types' /** * Helper function for defining a Sanity type definition. This function does not do anything on its own; @@ -171,7 +171,7 @@ import {type AllowOtherStrings} from './types' * @beta */ export function defineType< - const TType extends IntrinsicTypeName | AllowOtherStrings, + const TType extends IntrinsicTypeName | AutocompleteString, const TName extends string, TSelect extends Record | undefined, TPrepareValue extends Record | undefined, @@ -212,7 +212,7 @@ export function defineType< * @beta */ export function defineField< - const TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm) + const TType extends IntrinsicTypeName | AutocompleteString, const TName extends string, TSelect extends Record | undefined, TPrepareValue extends Record | undefined, @@ -255,7 +255,7 @@ export function defineField< * @beta */ export function defineArrayMember< - const TType extends string | IntrinsicTypeName, // IntrinsicTypeName here improves autocompletion in _some_ IDEs (not VS Code atm) + const TType extends IntrinsicTypeName | AutocompleteString, const TName extends string, TSelect extends Record | undefined, TPrepareValue extends Record | undefined, diff --git a/packages/@sanity/types/src/schema/types.ts b/packages/@sanity/types/src/schema/types.ts index adcaadd7858..6c80c216593 100644 --- a/packages/@sanity/types/src/schema/types.ts +++ b/packages/@sanity/types/src/schema/types.ts @@ -24,9 +24,20 @@ import {type PreviewConfig} from './preview' export {defineArrayMember, defineField, defineType, typed} from './define' -// Necessary since this is the only way to include all string literals and all other strings +/** + * Enhances VSCode autocomplete by using a distinct type for strings. + * + * `AllowOtherStrings` is defined as `string & {}`, an intersection that behaves + * like `string` but is treated differently by TypeScript's type system for + * internal processing. This helps in improving the specificity and relevance of + * autocomplete suggestions by potentially prioritizing `IntrinsicTypeName` + * over general string inputs, addressing issues where `string` type suggestions + * might overshadow more useful specific literals. + * + * @beta + */ // eslint-disable-next-line @typescript-eslint/ban-types -export type AllowOtherStrings = string & {} +export type AutocompleteString = string & {} /** * Note: you probably want `SchemaTypeDefinition` instead