From 63ea63defe6e3b593e3ef299f2181a0d01a25e63 Mon Sep 17 00:00:00 2001 From: s Date: Mon, 30 Nov 2020 09:04:25 -0500 Subject: [PATCH] fix(typings): discriminated types for widgets (#4640) --- packages/netlify-cms-core/index.d.ts | 120 +++++++++++++++++++++------ 1 file changed, 96 insertions(+), 24 deletions(-) diff --git a/packages/netlify-cms-core/index.d.ts b/packages/netlify-cms-core/index.d.ts index c50d61fa..3cc4152e 100644 --- a/packages/netlify-cms-core/index.d.ts +++ b/packages/netlify-cms-core/index.d.ts @@ -56,23 +56,29 @@ declare module 'netlify-cms-core' { default_locale?: string; } - export interface CmsField { + export interface CmsFieldBase { name: string; label?: string; - widget?: string; required?: boolean; hint?: string; pattern?: [string, string]; - default?: any; i18n?: boolean | 'translate' | 'duplicate'; + } + + export interface CmsFieldCode { + widget: 'code'; + default?: any; - /** If widget === "code" */ default_language?: string; allow_language_selection?: boolean; keys?: { code: string; lang: string }; output_code_only?: boolean; + } + + export interface CmsFieldDateTime { + widget: 'datetime'; + default?: string; - /** If widget === "datetime" */ format?: string; date_format?: boolean | string; time_format?: boolean | string; @@ -90,60 +96,102 @@ declare module 'netlify-cms-core' { * @deprecated Use picker_utc instead */ pickerUtc?: boolean; + } + + export interface CmsFieldFileOrImage { + widget: 'file' | 'image'; + default?: string; - /** If widget === "file" || widget === "image" */ media_library?: CmsMediaLibrary; allow_multiple?: boolean; config?: any; + } + + export interface CmsFieldObject { + widget: 'object'; + default?: any; - /** If widget === "object" || widget === "list" */ - fields?: CmsField[]; collapsed?: boolean; + summary?: string; + fields: CmsField[]; + } + + export interface CmsFieldList { + widget: 'list'; + default?: any; - /** If widget === "list" */ - field?: CmsField; allow_add?: boolean; + collapsed?: boolean; summary?: string; minimize_collapsed?: boolean; label_singular?: string; - types?: CmsField[]; + field?: CmsField; + fields?: CmsField[]; + max?: number; + min?: number; + add_to_top?: boolean; + types?: (CmsFieldBase & CmsFieldObject)[]; + } + + export interface CmsFieldMap { + widget: 'map'; + default?: string; - /** If widget === "map" */ decimals?: number; type?: CmsMapWidgetType; + } + + export interface CmsFieldMarkdown { + widget: 'markdown'; + default?: string; - /** If widget === "markdown" */ minimal?: boolean; buttons?: CmsMarkdownWidgetButton[]; editor_components?: string[]; + modes?: ('raw' | 'rich_text')[]; /** * @deprecated Use editor_components instead */ editorComponents?: string[]; + } + + export interface CmsFieldNumber { + widget: 'number'; + default?: string | number; - /** If widget === "number" */ value_type?: 'int' | 'float' | string; + min?: number; + max?: number; + step?: number; /** * @deprecated Use valueType instead */ valueType?: 'int' | 'float' | string; + } - /** If widget === "number" || widget === "select" */ + export interface CmsFieldSelect { + widget: 'select'; + default?: string | string[]; + + options: string[] | CmsSelectWidgetOptionObject[]; + multiple?: boolean; min?: number; max?: number; + } - /** If widget === "relation" || widget === "select" */ - multiple?: boolean; + export interface CmsFieldRelation { + widget: 'relation'; + default?: string | string[]; - /** If widget === "relation" */ - collection?: string; - value_field?: string; - search_fields?: string[]; + collection: string; + value_field: string; + search_fields: string[]; file?: string; display_fields?: string[]; + multiple?: boolean; options_length?: number; /** @@ -162,11 +210,35 @@ declare module 'netlify-cms-core' { * @deprecated Use options_length instead */ optionsLength?: number; - - /** If widget === "select" */ - options?: string[] | CmsSelectWidgetOptionObject[]; } + export interface CmsFieldHidden { + widget: 'hidden'; + default?: any; + } + + export interface CmsFieldStringOrText { + // This is the default widget, so declaring its type is optional. + widget?: 'string' | 'text'; + default?: string; + } + + export type CmsField = CmsFieldBase & + ( + | CmsFieldCode + | CmsFieldDateTime + | CmsFieldFileOrImage + | CmsFieldList + | CmsFieldMap + | CmsFieldMarkdown + | CmsFieldNumber + | CmsFieldObject + | CmsFieldRelation + | CmsFieldSelect + | CmsFieldHidden + | CmsFieldStringOrText + ); + export interface CmsCollectionFile { name: string; label: string;