Skip to content
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

docs: add typing global custom directives #2951

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions src/guide/reusability/custom-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,26 @@ When used on components, custom directives will always apply to a component's ro
```

Note that components can potentially have more than one root node. When applied to a multi-root component, a directive will be ignored and a warning will be thrown. Unlike attributes, directives can't be passed to a different element with `v-bind="$attrs"`.


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

In order to get autocompletion and type-checking for your global custom directives, you can extend the `ComponentCustomProperties` interface.

```ts
import { type Directive } from "vue"

declare module "vue" {
interface ComponentCustomProperties {
vCustomDirective: Directive<unknown, {foo: "value1" | "value2"}>
}
}
```

```vue-html
<!-- template of MyComponent -->

<div v-custom-directive="{ foo: 'value1' }"> <!-- autocompletion + type-check -->
<span>My component content</span>
</div>
```