Skip to content

Images has a wrong types attached to it #9549

Open
@firstaxel

Description

@firstaxel

Describe the Bug.

Good evening please i have been getting wrong types when i use upload field type in payload

export interface Testimonial {
  id: number;
  quote: string;
  name: string;
  designation: string;
  **src: number | Media;**
  updatedAt: string;
  createdAt: string;
}

I made emphasis on the src type generated for media in payload

it is gives me issue when i want to use typescript also i cant edit it cause it is generated everytime on a request is made to payload

Reproduction Steps

i created my schema like this one below
import type { CollectionConfig } from "payload"
import { Media } from "./Media"

export const CaseStudies: CollectionConfig = {
  slug: "case-studies",
  access: {
    read: () => true,
  },
  fields: [
    {
      name: "title",
      type: "text",
      required: true,
    },

    {
      name: "gallery",
      type: "array",
      fields: [
        {
          name: "image",
          type: "upload",
          relationTo: "media",
          required: true,
        },
      ],
    },
    {
      name: "link",
      type: "text",
    },
    {
      name: "description",
      type: "text",
    },
  ],
}

payload config

// storage-adapter-import-placeholder
import { postgresAdapter } from "@payloadcms/db-postgres"
import { payloadCloudPlugin } from "@payloadcms/payload-cloud"
import { lexicalEditor } from "@payloadcms/richtext-lexical"
import path from "path"
import { buildConfig } from "payload"
import { fileURLToPath } from "url"
import sharp from "sharp"
import { uploadthingStorage } from "@payloadcms/storage-uploadthing"

import { Users } from "./collections/Users"
import { Media } from "./collections/Media"
import { CaseStudies } from "./collections/CaseStudies"
import { Brands } from "./collections/Brands"
import { Testimonials } from "./collections/Testimonials"

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)

export default buildConfig({
  admin: {
    user: Users.slug,
    importMap: {
      baseDir: path.resolve(dirname),
    },
  },
  collections: [Users, Media, CaseStudies, Brands, Testimonials],
  editor: lexicalEditor(),
  secret: process.env.PAYLOAD_SECRET || "",
  typescript: {
    outputFile: path.resolve(dirname, "payload-types.ts"),
  },
  db: postgresAdapter({
    pool: {
      connectionString: process.env.DATABASE_URI || "",
    },
  }),
  sharp,
  plugins: [
    payloadCloudPlugin(),
    // storage-adapter-placeholder
    uploadthingStorage({
      collections: {
        media: true,
      },
      options: {
        token: process.env.UPLOADTHING_TOKEN,
        acl: "public-read",
      },
    }),
  ],
})

then i generate the type after put the schema in my payload config

types gotten are not useable

export interface Media {
  id: number;
  alt: string;
  _key?: string | null;
  updatedAt: string;
  createdAt: string;
  url?: string | null;
  thumbnailURL?: string | null;
  filename?: string | null;
  mimeType?: string | null;
  filesize?: number | null;
  width?: number | null;
  height?: number | null;
  focalX?: number | null;
  focalY?: number | null;
}
/**
 * This interface was referenced by `Config`'s JSON-Schema
 * via the `definition` "case-studies".
 */
export interface CaseStudy {
  id: number;
  title: string;
  gallery?:
    | {
        **image: number | Media;**
        id?: string | null;
      }[]
    | null;
  link?: string | null;
  description?: string | null;
  updatedAt: string;
  createdAt: string;
}

then i make a request to my payload backend in the nextjs app ND i get this error

Type 'import("/workspace/port/src/payload-types").Testimonial[]' is not assignable to type 'Testimonial[]'.
  Type 'import("/workspace/port/src/payload-types").Testimonial' is not assignable to type 'Testimonial'.
    Types of property 'src' are incompatible.
      Type 'number | Media' is not assignable to type 'string'.
        Type 'number' is not assignable to type 'string'.ts(2322)
animated-testimonials.tsx(18, 3): The expected type comes from property 'testimonials' which is declared here on type 'IntrinsicAttributes & { testimonials: Testimonial[]; autoplay?: boolean | undefined; }'
(property) testimonials: Testimonial[]

Environment Info

Node: 20.17.0
  npm: 10.8.2
  Yarn: 1.22.22
  pnpm: 9.9.0
Relevant Packages:
  payload: 3.0.2
  next: 15.0.3
  @payloadcms/db-postgres: 3.0.2
  @payloadcms/email-nodemailer: 3.0.2
  @payloadcms/graphql: 3.0.2
  @payloadcms/next/utilities: 3.0.2
  @payloadcms/payload-cloud: 3.0.2
  @payloadcms/plugin-cloud-storage: 3.0.2
  @payloadcms/richtext-lexical: 3.0.2
  @payloadcms/storage-uploadthing: 3.0.2
  @payloadcms/translations: 3.0.2
  @payloadcms/ui/shared: 3.0.2
  react: 19.0.0-rc-64f89510-20241119
  react-dom: 19.0.0-rc-64f89510-20241119
Operating System:
  Platform: linux
  Arch: x64
  Version: #202407021948 SMP PREEMPT_DYNAMIC Tue Jul  2 20:28:47 UTC 2024
  Available memory (MB): 64298
  Available CPU cores: 16

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: uiRelated to the admin panel.stalestatus: needs-triagePossible bug which hasn't been reproduced yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions