Skip to content

Commit

Permalink
types: improve app.directive type generics (#11926)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijkml authored Sep 16, 2024
1 parent aa5dafd commit 1bad606
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
19 changes: 12 additions & 7 deletions packages-private/dts-test/appDirective.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { expectType } from './utils'

const app = createApp({})

app.directive<HTMLElement, string>('custom', {
mounted(el, binding) {
expectType<HTMLElement>(el)
expectType<string>(binding.value)
app.directive<HTMLElement, string, 'prevent' | 'stop', 'arg1' | 'arg2'>(
'custom',
{
mounted(el, binding) {
expectType<HTMLElement>(el)
expectType<string>(binding.value)
expectType<{ prevent: boolean; stop: boolean }>(binding.modifiers)
expectType<'arg1' | 'arg2'>(binding.arg!)

// @ts-expect-error not any
expectType<number>(binding.value)
// @ts-expect-error not any
expectType<number>(binding.value)
},
},
})
)
19 changes: 17 additions & 2 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,23 @@ export interface App<HostElement = any> {
name: string,
component: T,
): this
directive<T = any, V = any>(name: string): Directive<T, V> | undefined
directive<T = any, V = any>(name: string, directive: Directive<T, V>): this
directive<
HostElement = any,
Value = any,
Modifiers extends string = string,
Arg extends string = string,
>(
name: string,
): Directive<HostElement, Value, Modifiers, Arg> | undefined
directive<
HostElement = any,
Value = any,
Modifiers extends string = string,
Arg extends string = string,
>(
name: string,
directive: Directive<HostElement, Value, Modifiers, Arg>,
): this
mount(
rootContainer: HostElement | string,
/**
Expand Down

0 comments on commit 1bad606

Please sign in to comment.