Interesting case for type inference

default discord avatar
\ ឵឵឵
7 months ago
4

I have some reusable

FieldHook

s where I am doing for example:


payload.findByID({
  collection: req.collection.config.slug,
  ...
})

Because of the new type inference scheme, which I do like very much,

req.collection.config.slug

no longer fits into the

collection

parameter of payload query operations:


Type 'string' is not assignable to type '"users" | ...'.ts(2322)
The expected type comes from property 'collection' which is declared here on type 'Options<"users" | ...>'

Marking it

as any

solves the problem for now, but curious if this might be something worth looking into.



Post weekend bump (;

  • discord user avatar
    jacobsfletch
    Payload Team
    7 months ago

    Ok so it sounds like

    req.collection.config.slug

    is typed as a generic string, but the local API will

    only

    except the exact slugs of your collections ("users", etc). Without looking at the code, this may be able to get resolved by improving the types at the hook-level to be a union of collection slugs. For now you might be available to import the slugs from your generated types to replace your

    as any

    type assertion.

  • default discord avatar
    \ ឵឵឵
    7 months ago

    Exactly. The goal is that the type of

    req.collection.config.slug

    is already that union, rather than remaining

    string

    .



    It would be great if that same typing could be applied to

    relationTo

    as well.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.