feat(types): support typing directive value via generic argument (#1007)

close #998
This commit is contained in:
underfin 2020-06-12 04:37:14 +08:00 committed by GitHub
parent e29f0b3fc2
commit 419b86d190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,18 +19,18 @@ import { currentRenderingInstance } from './componentRenderUtils'
import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling' import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
import { ComponentPublicInstance } from './componentProxy' import { ComponentPublicInstance } from './componentProxy'
export interface DirectiveBinding { export interface DirectiveBinding<V = any> {
instance: ComponentPublicInstance | null instance: ComponentPublicInstance | null
value: any value: V
oldValue: any oldValue: V | null
arg?: string arg?: string
modifiers: DirectiveModifiers modifiers: DirectiveModifiers
dir: ObjectDirective dir: ObjectDirective<any, V>
} }
export type DirectiveHook<T = any, Prev = VNode<any, T> | null> = ( export type DirectiveHook<T = any, Prev = VNode<any, T> | null, V = any> = (
el: T, el: T,
binding: DirectiveBinding, binding: DirectiveBinding<V>,
vnode: VNode<any, T>, vnode: VNode<any, T>,
prevVNode: Prev prevVNode: Prev
) => void ) => void
@ -40,19 +40,21 @@ export type SSRDirectiveHook = (
vnode: VNode vnode: VNode
) => Data | undefined ) => Data | undefined
export interface ObjectDirective<T = any> { export interface ObjectDirective<T = any, V = any> {
beforeMount?: DirectiveHook<T, null> beforeMount?: DirectiveHook<T, null, V>
mounted?: DirectiveHook<T, null> mounted?: DirectiveHook<T, null, V>
beforeUpdate?: DirectiveHook<T, VNode<any, T>> beforeUpdate?: DirectiveHook<T, VNode<any, T>, V>
updated?: DirectiveHook<T, VNode<any, T>> updated?: DirectiveHook<T, VNode<any, T>, V>
beforeUnmount?: DirectiveHook<T, null> beforeUnmount?: DirectiveHook<T, null, V>
unmounted?: DirectiveHook<T, null> unmounted?: DirectiveHook<T, null, V>
getSSRProps?: SSRDirectiveHook getSSRProps?: SSRDirectiveHook
} }
export type FunctionDirective<T = any> = DirectiveHook<T> export type FunctionDirective<T = any, V = any> = DirectiveHook<T, any, V>
export type Directive<T = any> = ObjectDirective<T> | FunctionDirective<T> export type Directive<T = any, V = any> =
| ObjectDirective<T, V>
| FunctionDirective<T, V>
export type DirectiveModifiers = Record<string, boolean> export type DirectiveModifiers = Record<string, boolean>