Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

What's the best way to add collections to your payload.config without running into TS issues?

default discord avatar
chocobutternutlast year
2

I have a

ChatMessages

collection in a different file, and a

Profile

collection in a different one.



I have

ChatMessages

as


export const ChatMessages: CollectionConfig = {
    slug: 'chat_messages',
    fields: [
    {
        name: 'user',
        type: 'relationship',
        relationTo: 'profile',
        required: false,
      },
    ],
  };

Then I have

Profile

as


  export const Profile: CollectionConfig = {
    slug: 'profile',
    fields: [
      ...
    ],
  };



I export them as an array called 'collections', then I spread them into the

collections

property of the buildConfig like so:


const collections = [Profile, ChatMessages];
buildConfig({ ..., collections: [...collections]})


But when I try to compile with yarn build, I get

Type '"profile"' is not assignable to type 'CollectionSlug | CollectionSlug[]'.

, in reference to the

relationTo

field in ChatMessages.



What am I doing wrong? 🤔

  • default discord avatar
    anders2292last year

    As far as I can see you have done this correctly. The config seems fine.



    That profile is not assignable to CollectionSlug is probably because your types are not generated, so typescript does not know about it yet.



    I see you use yarn so try

    yarn generate:types

    or

    yarn payload generate:types

    , it depends a bit on what is in your package.json how you run it.



    You find more details about it here:

    https://payloadcms.com/docs/typescript/generating-types
  • default discord avatar
    chocobutternutlast year

    Damn, didn't realize I forgot to generate the types. Thanks! That fixed it

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.