Skip to content

docs(directives): using global custom directives with typescript #3278

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/guide/reusability/custom-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ app.directive('highlight', {
})
```

It is possible to type global custom directives by extending the `ComponentCustomProperties` interface from `vue`

More Details: [Typing Custom Global Directives](/guide/typescript/composition-api#typing-global-custom-directives) <sup class="vt-badge ts" />

## When to use custom directives {#when-to-use}

Custom directives should only be used when the desired functionality can only be achieved via direct DOM manipulation.
Expand Down
41 changes: 40 additions & 1 deletion src/guide/typescript/composition-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@ import { useTemplateRef } from 'vue'
import MyGenericModal from './MyGenericModal.vue'
import type { ComponentExposed } from 'vue-component-type-helpers'

const modal = useTemplateRef<ComponentExposed<typeof MyGenericModal>>('modal')
const modal =
useTemplateRef<ComponentExposed<typeof MyGenericModal>>('modal')

const openModal = () => {
modal.value?.open('newValue')
Expand All @@ -477,3 +478,41 @@ const openModal = () => {
```

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.

## Typing Global Custom Directives {#typing-global-custom-directives}

In order to get type hints and type checking for global custom directives declared with `app.directive()`, you can extend `ComponentCustomProperties`

```ts [src/directives/highlight.ts]
import type { Directive } from 'vue'

export type HighlightDirective = Directive<HTMLElement, string>

declare module 'vue' {
export interface ComponentCustomProperties {
// prefix with v (v-highlight)
vHighlight: HighlightDirective
}
}

export default {
mounted: (el, binding) => {
el.style.backgroundColor = binding.value
}
} satisfies HighlightDirective
```

```ts [main.ts]
import highlight from './directives/highlight'
// ...other code
const app = createApp(App)
app.directive('highlight', highlight)
```

Usage in component

```vue [App.vue]
<template>
<p v-highlight="'blue'">This sentence is important!</p>
</template>
```
4 changes: 4 additions & 0 deletions src/guide/typescript/options-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,7 @@ The placement of this augmentation is subject to the [same restrictions](#type-a
See also:

- [TypeScript unit tests for component type extensions](https://github.com/vuejs/core/blob/main/packages-private/dts-test/componentTypeExtensions.test-d.tsx)

## Typing Global Custom Directives {#typing-global-custom-directives}

See: [Typing Custom Global Directives](/guide/typescript/composition-api#typing-global-custom-directives) <sup class="vt-badge ts" />