Skip to content

Commit c3f64b0

Browse files
committed
docs(typescript): using typescript with global custom directives
1 parent 549b330 commit c3f64b0

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/guide/reusability/custom-directives.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ app.directive('highlight', {
110110
})
111111
```
112112

113+
It is possible to type global custom directives by extending the `ComponentCustomProperties` interface from `vue`
114+
115+
More Details: [Typing Custom Global Directives](/guide/typescript/composition-api#typing-global-custom-directives) <sup class="vt-badge ts" />
116+
113117
## When to use custom directives {#when-to-use}
114118

115119
Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation.

src/guide/typescript/composition-api.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ import { useTemplateRef } from 'vue'
468468
import MyGenericModal from './MyGenericModal.vue'
469469
import 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
473474
const openModal = () => {
474475
modal.value?.open('newValue')
@@ -477,3 +478,44 @@ const openModal = () => {
477478
```
478479

479480
Note that with `@vue/language-tools` 2.1+, static template refs' types can be automatically inferred and the above is only needed in edge cases.
481+
482+
## Typing Global Custom Directives {#typing-global-custom-directives}
483+
484+
In order to get type hints and type checking for global custom directives declared with `app.directive()`, you can extend `ComponentCustomProperties`
485+
486+
```ts [src/directives/highlight.ts]
487+
// This can be directly added to any of your `.ts` files
488+
// It can also be added to a `.d.ts` file. Make sure it's included in
489+
// project's tsconfig.json "files"
490+
import type { Directive } from 'vue'
491+
492+
export type HighlightDirective = Directive<HTMLElement, string>
493+
494+
declare module 'vue' {
495+
export interface ComponentCustomProperties {
496+
// prefix with v (v-highlight)
497+
vHighlight: HighlightDirective
498+
}
499+
}
500+
501+
export default {
502+
mounted: (el, binding) => {
503+
el.style.backgroundColor = binding.value
504+
}
505+
} satisfies HighlightDirective
506+
```
507+
508+
```ts [main.ts]
509+
import highlight from './directives/highlight'
510+
// ...other code
511+
const app = createApp(App)
512+
app.directive('highlight', highlight)
513+
```
514+
515+
Usage in component
516+
517+
```vue [App.vue]
518+
<template>
519+
<p v-highlight="'blue'">This sentence is important!</p>
520+
</template>
521+
```

src/guide/typescript/options-api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,7 @@ The placement of this augmentation is subject to the [same restrictions](#type-a
291291
See also:
292292

293293
- [TypeScript unit tests for component type extensions](https://github.com/vuejs/core/blob/main/packages-private/dts-test/componentTypeExtensions.test-d.tsx)
294+
295+
## Typing Global Custom Directives {#typing-global-custom-directives}
296+
297+
See: [Typing Custom Global Directives](/guide/typescript/composition-api#typing-global-custom-directives) <sup class="vt-badge ts" />

0 commit comments

Comments
 (0)