From b650207fa5d3bc1763f67492574b2dfc32f2b435 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Tue, 25 Jul 2023 10:25:14 +0200 Subject: [PATCH] docs: add information on how to type custom hooks (#22312) --- docs/2.guide/3.going-further/2.hooks.md | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/2.guide/3.going-further/2.hooks.md b/docs/2.guide/3.going-further/2.hooks.md index d850c1d4a8..9b630f7d08 100644 --- a/docs/2.guide/3.going-further/2.hooks.md +++ b/docs/2.guide/3.going-further/2.hooks.md @@ -42,8 +42,8 @@ App hooks can be mainly used by [Nuxt Plugins](/docs/guide/directory-structure/p ```js [plugins/test.ts] export default defineNuxtPlugin((nuxtApp) => { nuxtApp.hook('page:start', () => { - /* your code goes here */ - }) + /* your code goes here */ + }) }) ``` @@ -73,3 +73,26 @@ export default defineNitroPlugin((nitroApp) => { ::alert{icon=👉} Learn more about available [Nitro lifecycle hooks](/docs/api/advanced/hooks#nitro-app-hooks-runtime-server-side). :: + +## Add additional hooks + +You can add additional hooks by augmenting the types provided by Nuxt. This can be useful for modules. + +```ts +import { HookResult } from "@nuxt/schema"; + +declare module '#app' { + interface RuntimeNuxtHooks { + 'your-nuxt-runtime-hook': () => HookResult + } + interface NuxtHooks { + 'your-nuxt-hook': () => HookResult + } +} + +declare module 'nitropack' { + interface NitroRuntimeHooks { + 'your-nitro-hook': () => void; + } +} +```