Skip to content

Migrate hooks package to TypeScript #70488

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 21 commits into from
Jul 11, 2025

Conversation

im3dabasia
Copy link
Contributor

What?

Part of: #67691
Migrating the hooks package to Typescript.

Why?

Type safety.

@im3dabasia im3dabasia marked this pull request as ready for review June 23, 2025 12:14
Copy link

github-actions bot commented Jun 23, 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: im3dabasia <im3dabasia1@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>

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

@im3dabasia im3dabasia changed the title [WIP] Migrate hooks package to TypeScript Migrate hooks package to TypeScript Jun 23, 2025
@t-hamano t-hamano added [Type] Code Quality Issues or PRs that relate to code quality [Package] Hooks /packages/hooks labels Jun 24, 2025
@t-hamano t-hamano mentioned this pull request Jun 24, 2025
37 tasks
Copy link
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

This looks good. I have some suggestions for improvement.

Comment on lines 9 to 24
export type Handler = {
callback: Callback;
namespace: string;
priority: number;
};

/**
* @typedef Hook
* @property {Handler[]} handlers Array of handlers
* @property {number} runs Run counter
*/
export type Hook = {
handlers: Handler[];
runs: number;
};

/**
* @typedef Current
* @property {string} name Hook name
* @property {number} currentIndex The index
*/
export type Current = {
name: string;
currentIndex: number;
};

Copy link
Member

Choose a reason for hiding this comment

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

Can we please move all these types to the types.ts file and then reuse them in createAddHook.ts

Then the types.ts file would look like this
import type { Hooks as internalHooks } from './createHooks';

export type Callback = ( ...args: any[] ) => any;

export type Handler = {
	callback: Callback;
	namespace: string;
	priority: number;
};

export type Hook = {
	handlers: Handler[];
	runs: number;
};

export type Current = {
	name: string;
	currentIndex: number;
};

export type Store = Record< string, Hook > & { __current: Set< Current > };

export type StoreKey = 'actions' | 'filters';

export type Hooks = internalHooks;

Then, we could add this to the index.ts

export * from './types';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be a good idea to also have the rest of types defined in other files be transferred to this central place? Would make the rest of the files cleaner.

PS: Talking about the following types: AddHook, DidHook, DoingHook, HasHook, RemoveHook, RunHook

What do you think 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Let us extract the shared ones for now.

@im3dabasia im3dabasia requested a review from manzoorwanijk July 8, 2025 05:38
Copy link
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

Let us properly use the extracted types

@im3dabasia
Copy link
Contributor Author

Since we are now importing directly from types.ts instead of from the index file, would it make sense to remove the following line?

All the imports in files that need these types are now referencing types.ts directly:

export * from './types';

@manzoorwanijk
Copy link
Member

Since we are now importing directly from types.ts instead of from the index file, would it make sense to remove the following line?

No, let us leave that export there to allow consumer components to import those types.

Copy link
Member

@manzoorwanijk manzoorwanijk left a comment

Choose a reason for hiding this comment

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

This looks pretty good now. Thank you for working on this.

@manzoorwanijk manzoorwanijk merged commit 7db8f38 into WordPress:trunk Jul 11, 2025
60 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.3 milestone Jul 11, 2025
cbravobernal pushed a commit that referenced this pull request Jul 15, 2025
Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org>
Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Hooks /packages/hooks [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants