contentful.js icon indicating copy to clipboard operation
contentful.js copied to clipboard

TS Typings suggest fields is never undefined, but it can be

Open s5no5t opened this issue 6 years ago • 3 comments

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

s5no5t avatar Nov 05 '19 20:11 s5no5t

cc @Remya-ecademy

s5no5t avatar Nov 05 '19 20:11 s5no5t

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.

jtomaszewski avatar Mar 31 '20 09:03 jtomaszewski

wouldn't it just make sense that we don't get back those entries at all via the CDN 🤷‍♂️ any progress on this ?

ovy9086 avatar Oct 21 '20 09:10 ovy9086

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.

marcolink avatar Jun 07 '23 06:06 marcolink