-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Migrate hooks
package to TypeScript
#70488
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. |
hooks
package to TypeScripthooks
package to TypeScript
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.
This looks good. I have some suggestions for improvement.
packages/hooks/src/index.ts
Outdated
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; | ||
}; | ||
|
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.
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';
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.
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 🤔
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.
Let us extract the shared ones for now.
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.
Let us properly use the extracted types
Since we are now importing directly from All the imports in files that need these types are now referencing types.ts directly: gutenberg/packages/hooks/src/index.ts Line 6 in d8c8cb9
|
No, let us leave that export there to allow consumer components to import those types. |
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.
This looks pretty good now. Thank you for working on this.
What?
Part of: #67691
Migrating the hooks package to Typescript.
Why?
Type safety.