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

Open
wants to merge 20 commits into
base: trunk
Choose a base branch
from

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
27 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
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