-
Notifications
You must be signed in to change notification settings - Fork 29
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
TypeScript Examples #33
Comments
Just realized that export type HookCallback = (...args: any) => Promise<void> | void https://github.com/unjs/hookable/blob/main/src/types.ts#L1 Is that expected? |
@pi0 any feedback would be most appreciated 🙂 |
Also I have noticed that although my JetBrains IDE seems to think that However one would simply go ahead and put a hooks
.callHook(Hook.BEFORE_SERIALIZED, msg)
.then(() => console.log("test")) This only works if two conditions are met...
hooks.hook(Hook.BEFORE_SERIALIZED, async (msg) => {
console.log("✅", msg.Message)
}) If any of these conditions are not met then a runtime error occurs (there is no The type definition callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): Promise<any>; should perhaps be callHook<NameT extends HookNameT>(name: NameT, ...args: Parameters<InferCallback<HooksT, NameT>>): void | Promise<any>; I edited the type def in Hookable directly in my IDE and the ignored promise warning is gone 🎉 We are building a library that exports the hooks in the config so it's quite normal to have unregistered hooks (noop). Regarding points 1... The core lib where Regarding point 2... The |
@danielroe Can you please help on this issue? 🙏🏼 |
Could anyone answer this For example: const msg = await app.callHook('user:login:before');
if (msg === undefined) {
Message.success('Hi, you are logged in!');
} else {
Message.error('Oops!' + msg);
return;
}
// ... @danielroe 🙏🙏 |
It would be really helpful to see typescript examples 👀 |
Some of working sample: import { Hookable } from 'hookable';
export class Parser extends Hookable<{
/** super tsdoc comment working too */
someOneHook: (rows: string[]) => void;
}> {
rows: string[] = [];
constructor() {
super(); // init instance of Hookable
}
getRows() {
this.rows = ['a', 'b', 'c'];
this.callHook('someOneHook', this.rows);
}
} import { Parser } from '...';
const parser = new Parser();
// subscribe
const unregisterSomeOneHook = parser.hook('someOneHook', rows => console.log(rows));
parser.getRows();
// unsubscribe
unregisterSomeOneHook(); |
Would be nice to have examples in the README for using types with Hookable e.g.
However I ran into an unexpected issue with the return type... if I set the type to the following it works without errors. But surely I need to specify the return type here right?
Argument of type '(val: string) => string' is not assignable to parameter of type 'never'.(2345)
Editor: https://stackblitz.com/edit/typescript-fkfd5n?devtoolsheight=33&file=index.ts
The text was updated successfully, but these errors were encountered: