Skip to content

Commit

Permalink
emit types
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD committed Mar 20, 2024
1 parent 06a6ef6 commit 2a389ed
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/api-generator/templates/directives.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DirectiveBinding } from 'vue'
import type { DirectiveBinding, ObjectDirective } from 'vue'
import type * as directives from '../../vuetify/lib/directives/index.d.mts'

type ExtractDirectiveBindings<T> = T extends object
Expand All @@ -11,7 +11,11 @@ type ExtractDirectiveBindings<T> = T extends object
}
: never
: never
: {}
: T[K] extends ObjectDirective<any, infer B>
? B extends object
? { value: B, modifiers: {} }
: never
: never
}
: never

Expand Down
19 changes: 15 additions & 4 deletions packages/vuetify/src/composables/directiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ import type {
ObjectDirective,
VNode,
} from 'vue'
import type { ComponentInstance } from '@/util'

export const useDirectiveComponent = (
component: string | Component,
props?: any
): ObjectDirective => {
type ExcludeProps =
| 'v-slots'
| `v-slot:${string}`
| `on${Uppercase<string>}${string}`
| 'key'
| 'ref'
| 'ref_for'
| 'ref_key'
| '$children'

export const useDirectiveComponent = <
C extends Component,
Props = Omit<ComponentInstance<C>['$props'], ExcludeProps>
>(component: string | C, props?: any): ObjectDirective<any, Props> => {
const concreteComponent = (typeof component === 'string'
? resolveComponent(component)
: component) as ConcreteComponent
Expand Down
28 changes: 28 additions & 0 deletions packages/vuetify/src/util/defineComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { propsFactory } from '@/util/propsFactory'
// Types
import type {
AllowedComponentProps,
Component,
ComponentCustomProps,
ComponentInjectOptions,
ComponentObjectPropsOptions,
Expand All @@ -20,6 +21,7 @@ import type {
ComponentOptionsWithObjectProps,
ComponentOptionsWithoutProps,
ComponentPropsOptions,
ComponentPublicInstance,
ComputedOptions,
DefineComponent,
EmitsOptions,
Expand Down Expand Up @@ -299,3 +301,29 @@ export interface FilterPropsOptions<PropsOptions extends Readonly<ComponentProps
U extends Exclude<keyof Props, Exclude<keyof Props, keyof T>>
> (props: T): Partial<Pick<T, U>>
}

// https:/vuejs/core/pull/10557
export type ComponentInstance<T> = T extends { new (): ComponentPublicInstance<any, any, any> }
? InstanceType<T>
: T extends FunctionalComponent<infer Props, infer Emits>
? ComponentPublicInstance<Props, {}, {}, {}, {}, ShortEmitsToObject<Emits>>
: T extends Component<
infer Props,
infer RawBindings,
infer D,
infer C,
infer M
>
? // NOTE we override Props/RawBindings/D to make sure is not `unknown`
ComponentPublicInstance<
unknown extends Props ? {} : Props,
unknown extends RawBindings ? {} : RawBindings,
unknown extends D ? {} : D,
C,
M
>
: never // not a vue Component

type ShortEmitsToObject<E> = E extends Record<string, any[]> ? {
[K in keyof E]: (...args: E[K]) => any;
} : E;

0 comments on commit 2a389ed

Please sign in to comment.