Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve dataview types #61586

Merged
merged 4 commits into from May 15, 2024
Merged

Conversation

johnhooks
Copy link
Contributor

What?

This commit improves the types in the dataviews package.

Why?

This is helpful by typing the item or items provided as arguments to the functions of the fields and actions of the DataViews.

This commit does not implement requiring these types in the DataView component, but helps to improve the types of the functions that are used in the DataView package. It also allows exporting the types to be used in consumers of the package.

How?

  • Add generic item type to all dataview types that operate on item or lists of items.

  • Update the usage in the dataviews package to incorporate the use of the generic item type.

  • Add an Action type for the dataview actions.

Testing Instructions

Building the package types.

Copy link

github-actions bot commented May 11, 2024

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.

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

Co-authored-by: johnhooks <bitmachina@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>

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

): { data: Data; paginationInfo: { totalItems: number; totalPages: number } } {
if ( ! data ) {
return {
data: EMPTY_ARRAY,
data: EMPTY_ARRAY as T[],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This as is required because EMPTY_ARRAY cannot be typed generically, though is used to provide a stable object reference for an empty data array.

Copy link
Member

Choose a reason for hiding this comment

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

What's the point of this? I'm assuming it's because EMPTY_ARRAY is stable?

I think we can change its type to [] and that will satisfy the return type without requiring this assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't used that before, I'll try it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That worked. Thanks @sirreal, I will be using that later.

Copy link

github-actions bot commented May 14, 2024

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Type-related labels to choose from: [Type] Automated Testing, [Type] Breaking Change, [Type] Bug, [Type] Build Tooling, [Type] Code Quality, [Type] Copy, [Type] Developer Documentation, [Type] Enhancement, [Type] Experimental, [Type] Feature, [Type] New API, [Type] Task, [Type] Technical Prototype, [Type] Performance, [Type] Project Management, [Type] Regression, [Type] Security, [Type] WP Core Ticket, Backport from WordPress Core.
  • Labels found: [Feature] Data Views.

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Thanks for taking a stab at improving the data views types.

packages/dataviews/src/types.ts Outdated Show resolved Hide resolved
packages/dataviews/src/normalize-fields.ts Outdated Show resolved Hide resolved
packages/dataviews/src/types.ts Outdated Show resolved Hide resolved
packages/dataviews/src/types.ts Outdated Show resolved Hide resolved
packages/dataviews/src/types.ts Show resolved Hide resolved
packages/dataviews/src/types.ts Outdated Show resolved Hide resolved
@TimothyBJacobs TimothyBJacobs added the [Feature] Data Views Work surrounding upgrading and evolving views in the site editor and beyond label May 14, 2024
@johnhooks johnhooks force-pushed the fix/dataview-types branch 2 times, most recently from 984a255 to 7e0a985 Compare May 14, 2024 23:08
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Thanks for these improvements.

This commit improves the types in the dataviews package.

This is helpful by typing the `item` or `items` provided as arguments
to the functions of the fields and actions of the `DataViews`.

This commit does not implement requiring these types in the `DataView`
component, but helps to improve the types of the functions that are
used in the `DataView` package. It also allows exporting the types
to be used in consumers of the package.

- Add generic item type to all dataview types that operate
on `item` or lists of `items`.

- Update the usage in the dataviews package to incorporate
the use of the generic item type.

- Add an `Action` type for the dataview actions.
This commit expands the original PR to include generic
types for `@wordpress/dataview` actions.

This commit also does the following:

- Applies several CR suggestions from @youknowriad and
@sirreal.
- Adds generic types to to all .ts/.tsx files in the dataviews.
- Renames `Item` to `AnyItem` to match the pattern of `@wordpress/data`.
- Name all generic types to `Item` which helps to make the code more readable,
  despite the fact TypeScript generics are in general cryptic.
@youknowriad youknowriad merged commit 44bca55 into WordPress:trunk May 15, 2024
59 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Data Views Work surrounding upgrading and evolving views in the site editor and beyond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants