-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix: improve dataview types #61586
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
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[], |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
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. |
There was a problem hiding this 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.
984a255
to
7e0a985
Compare
There was a problem hiding this 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.
f6c8792
to
8bdf02f
Compare
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.
8bdf02f
to
2176055
Compare
What?
This commit improves the types in the dataviews package.
Why?
This is helpful by typing the
item
oritems
provided as arguments to the functions of the fields and actions of theDataViews
.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 theDataView
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 ofitems
.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.