Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix autocomplete for defineType in VSCode #6447

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions packages/@sanity/types/src/schema/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type WidenValidation,
} from './defineTypes'
import {type FieldDefinitionBase, type IntrinsicTypeName} from './definition'
import {type AutocompleteString} from './types'

/**
* Helper function for defining a Sanity type definition. This function does not do anything on its own;
Expand Down Expand Up @@ -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 | AutocompleteString,
const TName extends string,
TSelect extends Record<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
Expand Down Expand Up @@ -211,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<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
Expand Down Expand Up @@ -254,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<string, string> | undefined,
TPrepareValue extends Record<keyof TSelect, any> | undefined,
Expand Down
15 changes: 15 additions & 0 deletions packages/@sanity/types/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ import {type PreviewConfig} from './preview'

export {defineArrayMember, defineField, defineType, typed} from './define'

/**
* 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 AutocompleteString = string & {}

/**
* Note: you probably want `SchemaTypeDefinition` instead
* @see SchemaTypeDefinition
Expand Down
Loading