@@ -468,7 +468,8 @@ import { useTemplateRef } from 'vue'
468468import MyGenericModal from './MyGenericModal.vue'
469469import type { ComponentExposed } from 'vue-component-type-helpers'
470470
471- const modal = useTemplateRef<ComponentExposed<typeof MyGenericModal>>('modal')
471+ const modal =
472+ useTemplateRef<ComponentExposed<typeof MyGenericModal>>('modal')
472473
473474const openModal = () => {
474475 modal.value?.open('newValue')
@@ -477,3 +478,41 @@ const openModal = () => {
477478```
478479
479480なお、` @vue/language-tools ` 2.1 以降では、静的テンプレート参照の型は自動的に推論されるので、上記はエッジケースでのみ必要となります。
481+
482+ ## カスタムグローバルディレクティブの型付け {#typing-global-custom-directives}
483+
484+ ` app.directive() ` で宣言されたグローバルカスタムディレクティブで型ヒントと型チェックを取得するために、` ComponentCustomProperties ` を拡張することができます
485+
486+ ``` ts [src/directives/highlight.ts]
487+ import type { Directive } from ' vue'
488+
489+ export type HighlightDirective = Directive <HTMLElement , string >
490+
491+ declare module ' vue' {
492+ export interface ComponentCustomProperties {
493+ // v をプレフィックスとして付ける(v-highlight)
494+ vHighlight: HighlightDirective
495+ }
496+ }
497+
498+ export default {
499+ mounted : (el , binding ) => {
500+ el .style .backgroundColor = binding .value
501+ }
502+ } satisfies HighlightDirective
503+ ```
504+
505+ ``` ts [main.ts]
506+ import highlight from ' ./directives/highlight'
507+ // ...その他のコード
508+ const app = createApp (App )
509+ app .directive (' highlight' , highlight )
510+ ```
511+
512+ コンポーネントでの使用方法
513+
514+ ``` vue [App.vue]
515+ <template>
516+ <p v-highlight="'blue'">This sentence is important!</p>
517+ </template>
518+ ```
0 commit comments