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.

Interesting case for type inference

default discord avatar
bcksl2 years ago
2

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
    2 years 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
    bcksl2 years 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.

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.