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.

Why does Payload generated types have numbers as a possibility in so many relations?

default discord avatar
pedgo12 months ago
2

A few examples from my generated types


image?: (number | null) | Image;

thumbnail?: (number | null) | Image;

youtube_channel: {
  link: string;
  name: string;
  image?: (number | null) | Image;
};

logo?: (number | null) | Image;

This makes my typescript code worse, not a real problem but I was wondering if it's intentional

  • discord user avatar
    alessiogr
    12 months ago

    100% intentional! That's because the data can be different depending on the depth.



    If you query your document with depth=0, then image will be a

    number

    (the id of the image).



    If you query it with depth=1, then image will be the populated image document (

    Image

    )



    And if it doesn't exist it will be

    null

    or undefined (

    ?

    )



    So with

    ?: (number | null) | Image

    you actually have the perfect type which covers all possibilities!

  • default discord avatar
    pedgo12 months ago

    hmmm this makes a lot of sense. Thank you.

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.