]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(types): support passing generics when registering global directives (#9660)
authorCarles Mitjans <carles.mitjans.coma@gmail.com>
Mon, 11 Dec 2023 14:07:24 +0000 (15:07 +0100)
committerGitHub <noreply@github.com>
Mon, 11 Dec 2023 14:07:24 +0000 (22:07 +0800)
packages/dts-test/appDirective.test-d.ts [new file with mode: 0644]
packages/runtime-core/src/apiCreateApp.ts
packages/runtime-core/src/compat/global.ts

diff --git a/packages/dts-test/appDirective.test-d.ts b/packages/dts-test/appDirective.test-d.ts
new file mode 100644 (file)
index 0000000..0d7707a
--- /dev/null
@@ -0,0 +1,14 @@
+import { createApp } from 'vue'
+import { expectType } from './utils'
+
+const app = createApp({})
+
+app.directive<HTMLElement, string>('custom', {
+  mounted(el, binding) {
+    expectType<HTMLElement>(el)
+    expectType<string>(binding.value)
+
+    // @ts-expect-error not any
+    expectType<number>(binding.value)
+  }
+})
index 8ba9429f447a55160bef533ff16f9d05a9475713..0afae35f0d1ce2e586b73b2cb6e1c9c83ed81c7f 100644 (file)
@@ -42,8 +42,8 @@ export interface App<HostElement = any> {
   mixin(mixin: ComponentOptions): this
   component(name: string): Component | undefined
   component(name: string, component: Component | DefineComponent): this
-  directive(name: string): Directive | undefined
-  directive(name: string, directive: Directive): this
+  directive<T = any, V = any>(name: string): Directive<T, V> | undefined
+  directive<T = any, V = any>(name: string, directive: Directive<T, V>): this
   mount(
     rootContainer: HostElement | string,
     isHydrate?: boolean,
index 0379bb67e8eddbbf7bfe21da34bab855b180c252..1e4d206eae3f361e87aa547f2fe84d646e7c8917 100644 (file)
@@ -82,8 +82,11 @@ export type CompatVue = Pick<App, 'version' | 'component' | 'directive'> & {
 
   component(name: string): Component | undefined
   component(name: string, component: Component): CompatVue
-  directive(name: string): Directive | undefined
-  directive(name: string, directive: Directive): CompatVue
+  directive<T = any, V = any>(name: string): Directive<T, V> | undefined
+  directive<T = any, V = any>(
+    name: string,
+    directive: Directive<T, V>
+  ): CompatVue
 
   compile(template: string): RenderFunction