Like what we’re doing? Star us on GitHub!

Typescript error on .create() of `upload collection`

Twoxic
3 weeks ago
3
const pMedia = await payload.create({
    collection: 'images',
    filePath: tmpFilePath,
    data: {
      name: 'name',
      alt: 'alt'
    }
  });


The above code works, but my IDE throws the following typescript error on the

data

property:


TS2322: Type '{ name: string; alt: string; }' is not assignable to type 'MarkOptional<Image, "id" | "updatedAt" | "createdAt">'.   Property 'sizes' is missing in type '{ name: string; alt: string; }' but required in type 'Omit<Image, "id" | "updatedAt" | "createdAt">'.


I copied the image collection from the docs (

https://payloadcms.com/docs/upload/overview

), I only added some

fields

. It looks like this:



const Images: CollectionConfig = {
  slug: 'images',
  access: {
    read: () => true
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true
    },
    {
      name: 'alt',
      type: 'text',
      required: true
    }
  ],
  upload: {
    staticURL: '/images',
    staticDir: imagesPath,
    imageSizes: [
      {
        name: 'thumbnail',
        width: 400,
        height: 300,
        position: 'centre'
      },
      {
        name: 'card',
        width: 768,
        height: 1024,
        position: 'centre'
      },
      {
        name: 'tablet',
        width: 1024,
        height: undefined,
        position: 'centre'
      }
    ],
    adminThumbnail: 'thumbnail',
    mimeTypes: ['image/*']
  }
};


Am I doing something wrong? Thanks in advance!



Hm. Looking at the documentation (

https://payloadcms.com/docs/upload/overview#image-sizes

) I gather that the

imageSizes

is used to automatically rescale the image (which it does). So why should I add

sizes

key to the

data

object? What is it used for?



Thanks for answering btw!

  • Jarrod
    Payload Team
    2 weeks ago

    @Twoxic I believe you are correct, it is a TS issue and not related to the shape of data you are sending. Sizes is created for you and you should not need to send it - we just need to adjust our types on upload collections create/update to not require

    sizes

    .

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More