A relationship field has the type string[] | MyCollection[] which makes sense, as it could change depending on the depth which is used.
Now, how would you best handle the value of this field without too much run-time type checking? I'm using:
let relationshipValue = (
await payload.findByID({
collection: 'sites',
id: docId,
depth: 0,
})
).relationship; // string[] | MyCollection[]
if (relationshipValue.length > 0 && typeof relationshipValue[0] === 'object') {
relationshipValue = relationshipValue.map((doc) => doc.id);
}
relationshipValue = relationshipValue as string[];
However, this feels unsafe and beyond dirty. Any other ideas? Might as well cast it to any if I have to use type assertions (as string[]) in the end