Skip to content

Commit 16352ca

Browse files
committed
chore: move arg after modifiers and add tests
1 parent ca63ffa commit 16352ca

File tree

3 files changed

+98
-30
lines changed

3 files changed

+98
-30
lines changed

packages/runtime-core/src/directives.ts

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
2020
import { ComponentPublicInstance } from './componentPublicInstance'
2121

2222
export interface DirectiveBinding<
23-
V = any,
24-
Arg extends string = string,
25-
Modifiers extends string = string
23+
Value = any,
24+
Modifiers extends string = string,
25+
Arg extends string = string
2626
> {
2727
instance: ComponentPublicInstance | null
28-
value: V
29-
oldValue: V | null
28+
value: Value
29+
oldValue: Value | null
3030
arg?: Arg
3131
modifiers: DirectiveModifiers<Modifiers>
32-
dir: ObjectDirective<any, V>
32+
dir: ObjectDirective<any, Value>
3333
}
3434

3535
export type DirectiveHook<
36-
T = any,
37-
Prev = VNode<any, T> | null,
38-
V = any,
39-
Arg extends string = string,
40-
Modifiers extends string = string
36+
HostElement = any,
37+
Prev = VNode<any, HostElement> | null,
38+
Value = any,
39+
Modifiers extends string = string,
40+
Arg extends string = string
4141
> = (
42-
el: T,
43-
binding: DirectiveBinding<V, Arg, Modifiers>,
44-
vnode: VNode<any, T>,
42+
el: HostElement,
43+
binding: DirectiveBinding<Value, Arg, Modifiers>,
44+
vnode: VNode<any, HostElement>,
4545
prevVNode: Prev
4646
) => void
4747

@@ -51,26 +51,48 @@ export type SSRDirectiveHook = (
5151
) => Data | undefined
5252

5353
export interface ObjectDirective<
54-
T = any,
55-
V = any,
56-
Arg extends string = string,
57-
Modifiers extends string = string
54+
HostElement = any,
55+
Value = any,
56+
Modifiers extends string = string,
57+
Arg extends string = string
5858
> {
59-
created?: DirectiveHook<T, null, V, Arg, Modifiers>
60-
beforeMount?: DirectiveHook<T, null, V, Arg, Modifiers>
61-
mounted?: DirectiveHook<T, null, V, Arg, Modifiers>
62-
beforeUpdate?: DirectiveHook<T, VNode<any, T>, V, Arg, Modifiers>
63-
updated?: DirectiveHook<T, VNode<any, T>, V, Arg, Modifiers>
64-
beforeUnmount?: DirectiveHook<T, null, V, Arg, Modifiers>
65-
unmounted?: DirectiveHook<T, null, V, Arg, Modifiers>
59+
created?: DirectiveHook<HostElement, null, Value, Arg, Modifiers>
60+
beforeMount?: DirectiveHook<HostElement, null, Value, Arg, Modifiers>
61+
mounted?: DirectiveHook<HostElement, null, Value, Arg, Modifiers>
62+
beforeUpdate?: DirectiveHook<
63+
HostElement,
64+
VNode<any, HostElement>,
65+
Value,
66+
Arg,
67+
Modifiers
68+
>
69+
updated?: DirectiveHook<
70+
HostElement,
71+
VNode<any, HostElement>,
72+
Value,
73+
Arg,
74+
Modifiers
75+
>
76+
beforeUnmount?: DirectiveHook<HostElement, null, Value, Arg, Modifiers>
77+
unmounted?: DirectiveHook<HostElement, null, Value, Arg, Modifiers>
6678
getSSRProps?: SSRDirectiveHook
6779
}
6880

69-
export type FunctionDirective<T = any, V = any> = DirectiveHook<T, any, V>
81+
export type FunctionDirective<
82+
HostElement = any,
83+
V = any,
84+
Modifiers extends string = string,
85+
Arg extends string = string
86+
> = DirectiveHook<HostElement, any, V, Arg, Modifiers>
7087

71-
export type Directive<T = any, V = any> =
72-
| ObjectDirective<T, V>
73-
| FunctionDirective<T, V>
88+
export type Directive<
89+
HostElement = any,
90+
Value = any,
91+
Modifiers extends string = string,
92+
Arg extends string = string
93+
> =
94+
| ObjectDirective<HostElement, Value, Arg, Modifiers>
95+
| FunctionDirective<HostElement, Value, Arg, Modifiers>
7496

7597
export type DirectiveModifiers<K extends string = string> = Record<K, boolean>
7698

packages/runtime-dom/src/directives/vModel.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function trigger(el: HTMLElement, type: string) {
4343
type ModelDirective<T, Modifiers extends string = string> = ObjectDirective<
4444
T & { _assign: AssignerFn },
4545
any,
46-
string,
4746
Modifiers
4847
>
4948

test-dts/directives.test-d.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Directive, expectError, expectType } from './index'
2+
3+
type ExtractBinding<T> = T extends (
4+
el: any,
5+
binding: infer B,
6+
vnode: any,
7+
prev: any
8+
) => any
9+
? B
10+
: never
11+
12+
declare function testDirective<
13+
Value,
14+
Modifiers extends string = string,
15+
Arg extends string = string
16+
>(): ExtractBinding<Directive<any, Value, Arg, Modifiers>>
17+
18+
expectType<{
19+
value: number
20+
oldValue: number | null
21+
arg?: 'Arg'
22+
modifiers: Record<'a' | 'b', boolean>
23+
}>(testDirective<number, 'a' | 'b', 'Arg'>())
24+
25+
expectError<{
26+
value: number
27+
oldValue: number | null
28+
arg?: 'Arg'
29+
modifiers: Record<'a' | 'b', boolean>
30+
// @ts-expect-error
31+
}>(testDirective<number, 'a', 'Arg'>())
32+
33+
expectType<{
34+
value: number
35+
oldValue: number | null
36+
arg?: 'Arg'
37+
modifiers: Record<'a' | 'b', boolean>
38+
// @ts-expect-error
39+
}>(testDirective<number, 'a' | 'b', 'Argx'>())
40+
41+
expectType<{
42+
value: number
43+
oldValue: number | null
44+
arg?: 'Arg'
45+
modifiers: Record<'a' | 'b', boolean>
46+
// @ts-expect-error
47+
}>(testDirective<string, 'a' | 'b', 'Arg'>())

0 commit comments

Comments
 (0)