Skip to content

Commit

Permalink
feat(types): align ComponentPublicInstance type
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 21, 2021
1 parent 77e5f0c commit 2f9cfbf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
53 changes: 52 additions & 1 deletion src/component/componentProxy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ExtractDefaultPropTypes, ExtractPropTypes } from './componentProps'
import { ShallowUnwrapRef } from '..'
import {
nextTick,
ShallowUnwrapRef,
UnwrapNestedRefs,
WatchOptions,
WatchStopHandle,
} from '..'
import { Data } from './common'

import Vue, {
Expand All @@ -11,6 +17,12 @@ import {
MethodOptions,
ExtractComputedReturns,
} from './componentOptions'
import {
ComponentInternalInstance,
EmitFn,
EmitsOptions,
Slots,
} from '../runtimeContext'

export type ComponentInstance = InstanceType<VueConstructor>

Expand Down Expand Up @@ -79,3 +91,42 @@ export type VueProxy<
ExtractPropTypes<PropsOptions>
> &
VueConstructorProxy<PropsOptions, RawBindings, Data, Computed, Methods>

// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
P = {}, // props type extracted from props option
B = {}, // raw bindings returned from setup()
D = {}, // return from data()
C extends ComputedOptions = {},
M extends MethodOptions = {},
E extends EmitsOptions = {},
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false
> = {
$: ComponentInternalInstance
$data: D
$props: MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
$attrs: Data
$refs: Data
$slots: Slots
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
$emit: EmitFn<E>
$el: any
// $options: Options & MergedComponentOptionsOverride
$forceUpdate: () => void
$nextTick: typeof nextTick
$watch(
source: string | Function,
cb: Function,
options?: WatchOptions
): WatchStopHandle
} & P &
ShallowUnwrapRef<B> &
UnwrapNestedRefs<D> &
ExtractComputedReturns<C> &
M
6 changes: 5 additions & 1 deletion src/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export {
MethodOptions,
ComponentPropsOptions,
} from './componentOptions'
export { ComponentInstance, ComponentRenderProxy } from './componentProxy'
export {
ComponentInstance,
ComponentPublicInstance,
ComponentRenderProxy,
} from './componentProxy'
export { Data } from './common'
export {
PropType,
Expand Down
2 changes: 2 additions & 0 deletions src/runtimeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export interface SetupContext<E = EmitsOptions> {
readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] }
}

export interface ComponentPublicInstance {}

/**
* We expose a subset of properties on the internal instance as they are
* useful for advanced external libraries and tools.
Expand Down

0 comments on commit 2f9cfbf

Please sign in to comment.