contentful.js
contentful.js copied to clipboard
TS Typings suggest fields is never undefined, but it can be
The typings for Entry suggest that the fields property cannot be undefined: https://github.com/contentful/contentful.js/blob/559733a47b2dcf8918f46c15f7d10ae366f09e49/index.d.ts#L82
That isn't always true: fields is undefined if there is a draft entry that is referenced from a published entry, i.e.
A (published) -----> B (draft)
When querying for A, A.b_ref.fields is undefined. This happens when someone force-publishes A by ignoring the warning in the Contentful UI.
This can easily break production apps (as it did for us), because ignoring the warning is easy, and force-publishing cannot be disabled completely.
Therefore the typings should be changed so that Entry<T>.fields is of T | undefined
cc @Remya-ecademy
True. We encountered this problem [luckily, on staging env] just now in the same way as you did.
Seems like, the easy workaround is to pass removeUnresolved: true into your contentful.createClient() method. See https://github.com/contentful/contentful.js/issues/214 for explanation.
Whether the TS typedefs should be improved, that's still a valid question though.
P.S. Why isn't removeUnresolved: true set as a default option? I think it would be a more reasonable default.
wouldn't it just make sense that we don't get back those entries at all via the CDN 🤷♂️ any progress on this ?
Hey @s5no5t 👋 , We expected this to be solved with the latest major release (which was a complete re-write). Please feel free to re-open again if the error still occurs.