-
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
[core-data] Document and add types for dynamic actions and selectors. #67668
base: trunk
Are you sure you want to change the base?
[core-data] Document and add types for dynamic actions and selectors. #67668
Conversation
…namic-actions-selectors-for-core-data
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. |
Size Change: 0 B Total Size: 1.84 MB ℹ️ View Unchanged
|
declare module './base-entity-records' { | ||
export namespace BaseEntityRecords { | ||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ | ||
export interface PostType< C extends Context > { |
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.
The description of these fields is copied from core REST API schema.
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.
Tip: there's a ton of pre-existing type definitions at https://github.com/johnbillion/wp-json-schemas/tree/trunk/packages/wp-types
Flaky tests detected in 5a09cec. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12512865171
|
* @param data The comment data | ||
* @param options | ||
*/ | ||
export declare function saveComment( |
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'm not sure it's a good idea to hardcode all these. Even if we listed them all (which I'm not sure we did) how can we guarantee they're up to date if we add a new entity in the future? Furthermore, some of those are actually dynamic, based on actual registered post types and taxonomies. We don't seem to consider those here, do we? With that in mind, we might want to make the type a bit broader. Knowing how tricky it has been historically to type custom Redux implementations, this can be a fun challenge. cc @jsnajdr for thoughts
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.
Agreed. Could we make these somehow dynamic based on EntityRecord
or so? And then maybe make them generic so that anyone with custom post types could add them on top?
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 am not sure of a better way of informing TypeScript that we have defined something dynamically using Proxy
.
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.
Perhaps if we keep the types more open in a dictionary, and just provide some signatures like save*
and delete*
for actions and get*
for selectors? See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#symbol-and-template-string-pattern-index-signatures
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.
Alright, that is going to test my TS skills. I am up for the challenge. 😄
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.
OK, I have done it. Mission accomplished 😄
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.
Nice, thanks for taking it 😅 I'll try to get you another review next week.
Fixes #56287 #67847
Related #67142
What?
Define the type declarations for dynamic selectors and actions for
core-data
package`Why?
The dynamic selectors cause errors when used in TypeScript because TS can't find those defined anywhere.
How?
The PR creates TS definition declarations for all the dynamic selectors and some useful actions
Testing Instructions
Example
Testing Instructions for Keyboard
Screenshots or screencast
We will now have auto-completions for entity selectors