Skip to content

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

Merged
merged 8 commits into from
Jun 18, 2025

Conversation

DAreRodz
Copy link
Contributor

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> and TypeYield<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:

  1. Circular References: If state is used within a yield expression or if the generator's return value depends on state, TypeScript can struggle to infer types, leading to circular reference errors or types defaulting to any.
  2. Typing Yielded Values: Even if the overall generator type is correctly inferred or specified, the actual value resolved by an individual yield expression (e.g., the result of a fetched promise) is not accurately typed within the generator's scope, defaulting to any.

How?

  • AsyncAction<ReturnType>: Defined as Generator<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 when state is used within yield expressions or in the final return value.

  • TypeYield<T extends (...args: any[]) => Promise<any>>: Defined as Awaited<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.

@DAreRodz DAreRodz added [Type] Enhancement A suggestion for improvement. [Feature] Interactivity API API to add frontend interactivity to blocks. [Packages] Interactivity /packages/interactivity labels Jun 13, 2025
Copy link

github-actions bot commented Jun 13, 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.

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: luisherranz <luisherranz@git.wordpress.org>
Co-authored-by: DAreRodz <darerodz@git.wordpress.org>

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

Copy link
Member

@luisherranz luisherranz left a comment

Choose a reason for hiding this comment

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

LGTM!

Base automatically changed from feature/iapi-new-client-side-navigation to trunk June 18, 2025 07:20
@DAreRodz DAreRodz force-pushed the backport/iapi-add-type-helpers-for-async-actions branch from 5bcf8cd to e6bcb9f Compare June 18, 2025 07:26
Copy link

Flaky tests detected in e6bcb9f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/15726592600
📝 Reported issues:

@DAreRodz DAreRodz enabled auto-merge (squash) June 18, 2025 07:55
@DAreRodz DAreRodz merged commit 86008b1 into trunk Jun 18, 2025
73 of 91 checks passed
@DAreRodz DAreRodz deleted the backport/iapi-add-type-helpers-for-async-actions branch June 18, 2025 08:02
@github-actions github-actions bot added this to the Gutenberg 21.1 milestone Jun 18, 2025
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Interactivity API API to add frontend interactivity to blocks. [Packages] Interactivity /packages/interactivity [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants