Really appreciate the outstanding work on Local API type inferences! Experiencing a small inconvenience, and wondering if there's an obvious solution I'm missing here.
When requesting a relation field, the return type of the field would be
string | Collection
Is there a way to get post, such that the inferred type of post.notes is Note only?
I tried with depth:, but that doesn't seem to make a difference.
This is one thing we're talking about improving. Possibly passing depth, so the type will be able to properly be inferred as one or the other. More to come!
Because it could be either based upon depth, it needed to be a union.
Glad to hear!
Luckily this can be worked around
(post.note as Note).map
post.notes.map(note => typeof note === "string" ? ... : ...)
Any updates on this?
We haven't made any definitive moves on this. Related conversations are located here:https://github.com/payloadcms/payload/discussions/1563
For the time being, you'll need to use TypeScript type-narrowing. Happy to provide some examples or assistance if you're running into something specific 👍
The depth option at the type level seems tricky. Thanks, think type narrowing will be fine for my use case
Yep, usually it will come down to doing a
typeof myValue === 'string'
typeof myValue === 'object'
depending on if you're checking it as an ID or the actual collection valuve
FWIW, my workaround was a script similar to what @alamit postedhttps://github.com/payloadcms/payload/discussions/1563#discussioncomment-5357724