-
Notifications
You must be signed in to change notification settings - Fork 4.5k
iAPI: Introduce AsyncAction
and TypeYield
type helpers
#70422
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
iAPI: Introduce AsyncAction
and TypeYield
type helpers
#70422
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. |
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.
LGTM!
5bcf8cd
to
e6bcb9f
Compare
Flaky tests detected in e6bcb9f. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/15726592600
|
…#70422) * Introduce AsyncAction and TypeYield helpers * Use the helpers on the other tests * Update docs for AsyncAction * Update docs for TypeYield * Export the helpers * Replace manual typing with satisfies on TypeYield return * Improve docs * Update changelog --------- Co-authored-by: luisherranz <luisherranz@git.wordpress.org> Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
What?
Warning
This PR has been created on top of #70353. Wait for that PR to be merged before merging this one.
This PR introduces two new TypeScript helper types,
AsyncAction<ReturnType>
andTypeYield<T>
, to the Interactivity API.It also adds documentation for both helpers in the "Using TypeScript" guide, explaining their purpose and how to use them to solve common typing issues with asynchronous actions (generators).
Props to @luisherranz.
Why?
When working with asynchronous actions (defined as generators) in the Interactivity API and TypeScript, developers can encounter two main issues:
state
is used within ayield
expression or if the generator's return value depends onstate
, TypeScript can struggle to infer types, leading to circular reference errors or types defaulting toany
.yield
expression (e.g., the result of a fetched promise) is not accurately typed within the generator's scope, defaulting toany
.How?
AsyncAction<ReturnType>
: Defined asGenerator<any, ReturnType, unknown>
.This helper allows developers to explicitly type the return value of an asynchronous action (generator). By using
any
for the yielded values, it helps break circular type dependencies whenstate
is used withinyield
expressions or in the final return value.TypeYield<T extends (...args: any[]) => Promise<any>>
: Defined asAwaited<ReturnType<T>>
.This helper allows developers to explicitly type the value that a
yield
expression resolves to, by providing the type of the async function/operation being yielded.