diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index b80703dcc67b1..e740998a23623 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -178,7 +178,7 @@ _Parameters_ _Returns_ -- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits. +- `undefined< EntityRecord > | false`: The entity record, merged with its edits. ### getEmbedPreview diff --git a/packages/core-data/README.md b/packages/core-data/README.md index 6677a32df08dc..444bce35ee521 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -499,7 +499,7 @@ _Parameters_ _Returns_ -- `undefined< EntityRecord > | undefined`: The entity record, merged with its edits. +- `undefined< EntityRecord > | false`: The entity record, merged with its edits. ### getEmbedPreview diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index 49964339a4f2b..1bfb13f38ac15 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -45,7 +45,7 @@ describe( 'useEntityRecord', () => { expect( data ).toEqual( { edit: expect.any( Function ), - editedRecord: {}, + editedRecord: false, hasEdits: false, edits: {}, record: undefined, diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 4a135f343a097..d238438e10eb1 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -846,10 +846,21 @@ export const getEditedEntityRecord = createSelector( kind: string, name: string, recordId: EntityRecordKey - ): ET.Updatable< EntityRecord > | undefined => ( { - ...getRawEntityRecord( state, kind, name, recordId ), - ...getEntityRecordEdits( state, kind, name, recordId ), - } ), + ): ET.Updatable< EntityRecord > | false => { + const raw = getRawEntityRecord( state, kind, name, recordId ); + const edited = getEntityRecordEdits( state, kind, name, recordId ); + // Never return a non-falsy empty object. Unfortunately we can't return + // undefined or null because we were previously returning an empty + // object, so trying to read properties from the result would throw. + // Using false here is a workaround to avoid breaking changes. + if ( ! raw && ! edited ) { + return false; + } + return { + ...raw, + ...edited, + }; + }, ( state: State, kind: string, @@ -1264,7 +1275,7 @@ export function getReferenceByDistinctEdits( state ) { export function __experimentalGetTemplateForLink( state: State, link: string -): Optional< ET.Updatable< ET.WpTemplate > > | null { +): Optional< ET.Updatable< ET.WpTemplate > > | null | false { const records = getEntityRecords< ET.WpTemplate >( state, 'postType',