Skip to content

Commit 678a4b3

Browse files
authored
feat: add directives type support (#863)
1 parent b3e61a4 commit 678a4b3

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/apis/createApp.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type Vue from 'vue'
22
import { VueConstructor } from 'vue'
3+
import { Directive } from '../component/directives'
34
import { getVueConstructor } from '../runtimeContext'
45
import { warn } from '../utils'
56

@@ -9,7 +10,8 @@ export interface App<T = any> {
910
use: VueConstructor['use']
1011
mixin: VueConstructor['mixin']
1112
component: VueConstructor['component']
12-
directive: VueConstructor['directive']
13+
directive(name: string): Directive | undefined
14+
directive(name: string, directive: Directive): this
1315
mount: Vue['$mount']
1416
unmount: Vue['$destroy']
1517
}
@@ -19,12 +21,19 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App {
1921

2022
let mountedVM: Vue | undefined = undefined
2123

22-
return {
24+
const app: App = {
2325
config: V.config,
2426
use: V.use.bind(V),
2527
mixin: V.mixin.bind(V),
2628
component: V.component.bind(V),
27-
directive: V.directive.bind(V),
29+
directive(name: string, dir?: Directive | undefined): any {
30+
if (dir) {
31+
V.directive(name, dir as any)
32+
return app
33+
} else {
34+
return V.directive(name)
35+
}
36+
},
2837
mount: (el, hydrating) => {
2938
if (!mountedVM) {
3039
mountedVM = new V({ propsData: rootProps, ...rootComponent })
@@ -51,4 +60,5 @@ export function createApp(rootComponent: any, rootProps: any = undefined): App {
5160
}
5261
},
5362
}
63+
return app
5464
}

src/component/directives.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { VNodeDirective, VNode } from 'vue'
2+
3+
export type DirectiveModifiers = Record<string, boolean>
4+
5+
export interface DirectiveBinding<V> extends Readonly<VNodeDirective> {
6+
readonly modifiers: DirectiveModifiers
7+
readonly value: V
8+
readonly oldValue: V | null
9+
}
10+
11+
export type DirectiveHook<T = any, Prev = VNode | null, V = any> = (
12+
el: T,
13+
binding: DirectiveBinding<V>,
14+
vnode: VNode,
15+
prevVNode: Prev
16+
) => void
17+
18+
export interface ObjectDirective<T = any, V = any> {
19+
bind?: DirectiveHook<T, any, V>
20+
inserted?: DirectiveHook<T, any, V>
21+
update?: DirectiveHook<T, any, V>
22+
componentUpdated?: DirectiveHook<T, any, V>
23+
unbind?: DirectiveHook<T, any, V>
24+
}
25+
export type FunctionDirective<T = any, V = any> = DirectiveHook<T, any, V>
26+
27+
export type Directive<T = any, V = any> =
28+
| ObjectDirective<T, V>
29+
| FunctionDirective<T, V>

src/component/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ export {
1818
ExtractPropTypes,
1919
ExtractDefaultPropTypes,
2020
} from './componentProps'
21+
22+
export {
23+
DirectiveModifiers,
24+
DirectiveBinding,
25+
DirectiveHook,
26+
ObjectDirective,
27+
FunctionDirective,
28+
} from './directives'

0 commit comments

Comments
 (0)