#Interesting case for type inference

4 messages · Page 1 of 1 (latest)

brazen furnace
#

I have some reusable FieldHooks 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.

brazen furnace
#

Post weekend bump (;

gleaming flame
#

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.

brazen furnace
#

Exactly. The goal is that the type of req.collection.config.slug is already that union, rather than remaining string.