decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.69k stars 3.03k forks source link

Typescript: Object literal may only specify known properties, and 'min' does not exist in type #6800

Open kidroca opened 1 year ago

kidroca commented 1 year ago

Describe the bug

If we define a relation field with multiple: true and min or max we get the following typescript typings error

Object literal may only specify known properties, and 'min' does not exist in type '(CmsFieldBase & CmsFieldRelation) | (CmsFieldBase & CmsFieldMeta)'.

To Reproduce

  1. Have a project using Typescript
  2. Have your CMS schema defined in typescript

Sample code:

import CMS from 'netlify-cms-app';

const local_backend = process.env.NODE_ENV === 'development';

CMS.init({
  config: {
    local_backend,
    load_config_file: false,
    backend: {
      name: 'git-gateway',
      branch: 'main',
    },
    collections: [
      {
        name: 'pages',
        label: 'Pages',
        folder: 'cms/content/pages',
        create: false,
        slug: '{{slug}}',
        fields: [
          {
            label: 'Fund Types',
            name: 'fundTypes',
            widget: 'relation',
            collection: 'Options',
            search_fields: ['label'],
            value_field: 'label',
            multiple: true,
            min: 1, // we get the error here
          },
        ],
      },
    ],
  },
});

Expected behavior

No typescript error should occur, because min and max are valid relation properties

Screenshots

Applicable Versions:

CMS configuration

import CMS from 'netlify-cms-app';

const local_backend = process.env.NODE_ENV === 'development';

CMS.init({
  config: {
    local_backend,
    load_config_file: false,
    backend: {
      name: 'git-gateway',
      branch: 'main',
    },
    collections: [
      {
        name: 'pages',
        label: 'Pages',
        folder: 'cms/content/pages',
        create: false,
        slug: '{{slug}}',
        fields: [
          {
            label: 'Fund Types',
            name: 'fundTypes',
            widget: 'relation',
            collection: 'Options',
            search_fields: ['label'],
            value_field: 'label',
            multiple: true,
            min: 1, // we get the error here
          },
        ],
      },
    ],
  },
});

Additional context

kidroca commented 1 year ago

It seems min and max are not part of the CmsFieldRelation interface https://github.com/decaporg/decap-cms/blob/5d33d098db7917a8e416f4a634db5aaf13f60cbb/packages/netlify-cms-core/index.d.ts#L204-L232

The fields are available on the CmsFieldList interface https://github.com/decaporg/decap-cms/blob/5d33d098db7917a8e416f4a634db5aaf13f60cbb/packages/netlify-cms-core/index.d.ts#L149-L150

And on the CmsFieldSelect interface https://github.com/decaporg/decap-cms/blob/5d33d098db7917a8e416f4a634db5aaf13f60cbb/packages/netlify-cms-core/index.d.ts#L200-L201