Skip to content

Export UseEntityRecordsWithPermissionsType #71003

Merged
Mamaduka merged 9 commits intoWordPress:trunkfrom
xavier-lc:add/export-UseEntityRecordsWithPermissionsType
Sep 10, 2025
Merged

Export UseEntityRecordsWithPermissionsType #71003
Mamaduka merged 9 commits intoWordPress:trunkfrom
xavier-lc:add/export-UseEntityRecordsWithPermissionsType

Conversation

@xavier-lc
Copy link
Contributor

@xavier-lc xavier-lc commented Jul 31, 2025

What?

Closes

This PR exposes the WithPermissions so you can get proper typing on the useEntityRecordsWithPermissions function after unlocking core-data private APIs.

Why?

Unlocking core-data private APIs leaves you without typing for the unlocked function:

image

How?

Add the necessary export on the core-data package.

Testing Instructions

  1. Checkout this branch locally
  2. Download gutenberg-71003.patch from this gist
  3. git apply gutenberg-71003.patch
  4. Open the tryit.ts file with your editor and check that the unlocked useEntityRecordsWithPermissions has proper typing:
image

@xavier-lc xavier-lc requested a review from nerrad as a code owner July 31, 2025 15:17
@github-actions
Copy link

github-actions bot commented Jul 31, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @xavier.lozano.carreras@a8c.com.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: xavier.lozano.carreras@a8c.com.

Co-authored-by: dsas <dsas@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
Co-authored-by: xavier-lc <xavilc@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jul 31, 2025
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @xavier-lc! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@xavier-lc xavier-lc changed the title [WiP] Export use entity records with permissions type Export UseEntityRecordsWithPermissionsType Jul 31, 2025
@dsas dsas added the [Type] Code Quality Issues or PRs that relate to code quality label Aug 22, 2025
Copy link
Contributor

@dsas dsas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works as described, I can't see any reason not to merge it but I'm not an expert here :)

* } >( coreDataPrivateApis );
* ```
*/
export type { UseEntityRecordsWithPermissionsType };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it feels off to export a type publicly while the actual API you're typing is private.

cc @Mamaduka: do you know if we are looking at exposing useEntityRecordsWithPermissions publicly at some point?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems odd to have exported a public type for a private API.

I don't know if we plan to make useEntityRecordsWithPermissions public. The DataViews might evolve differently and make this hook obsolete.

cc @youknowriad

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does feel odd indeed, that said I don't really consider type additions/modification/removals as breaking changes in terms of WP API, these are not available in the wp globals which means I don't mind if the type is exposed publicly at the moment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, having a type evailable publicly, doesn't hurt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end result is this

export type UseEntityRecordsWithPermissionsType = typeof useEntityRecordsWithPermissions;

which I think is counter intuitive.

Consumer code can do that easily and thus naming something as ***Type and exporting it doesn't make much sense to me.

I do like the other changes made in this PR though.

So, my suggestions would be to not export the type as a convenience, but keep the other changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The end result is this

export type UseEntityRecordsWithPermissionsType = typeof useEntityRecordsWithPermissions;

which I think is counter intuitive.

Consumer code can do that easily and thus naming something as ***Type and exporting it doesn't make much sense to me.

@manzoorwanijk I added the UseEntityRecordsWithPermissionsType export because useEntityRecordsWithPermissions is only available from the outside after the unlock call, and its type is any, so consumer code cannot rely on typeof useEntityRecordsWithPermissions.

If we keep the WithPermissions export, though, this can be done from the outside:

import { unlock } from '@wordpress/admin-toolkit';
import {
	WithPermissions,
	useEntityRecords,
	privateApis,
} from '@wordpress/core-data';

interface EntityRecordsWithPermissionsResolution< RecordType >
	extends Omit<
		ReturnType< typeof useEntityRecords< RecordType > >,
		'records'
	> {
	/** The requested entity records with permissions */
	records: WithPermissions< RecordType >[] | null;
}

const { useEntityRecordsWithPermissions } = unlock< {
	useEntityRecordsWithPermissions: UseEntityRecordsWithPermissionsType;
} >( privateApis );

Would that be good?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that instead of exporting the *Type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manzoorwanijk thanks, I've removed the export and updated the example gist and description.

Copy link
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me now.

@Mamaduka Mamaduka merged commit a13fcbe into WordPress:trunk Sep 10, 2025
78 of 79 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.7 milestone Sep 10, 2025
talldan pushed a commit that referenced this pull request Sep 10, 2025
Unlinked contributors: xavier.lozano.carreras@a8c.com.

Co-authored-by: dsas <dsas@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
Co-authored-by: xavier-lc <xavilc@git.wordpress.org>
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Sep 11, 2025
Unlinked contributors: xavier.lozano.carreras@a8c.com.

Co-authored-by: dsas <dsas@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
Co-authored-by: xavier-lc <xavilc@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants