Skip to content

Commit 2f9cfbf

Browse files
committed
feat(types): align ComponentPublicInstance type
1 parent 77e5f0c commit 2f9cfbf

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/component/componentProxy.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ExtractDefaultPropTypes, ExtractPropTypes } from './componentProps'
2-
import { ShallowUnwrapRef } from '..'
2+
import {
3+
nextTick,
4+
ShallowUnwrapRef,
5+
UnwrapNestedRefs,
6+
WatchOptions,
7+
WatchStopHandle,
8+
} from '..'
39
import { Data } from './common'
410

511
import Vue, {
@@ -11,6 +17,12 @@ import {
1117
MethodOptions,
1218
ExtractComputedReturns,
1319
} from './componentOptions'
20+
import {
21+
ComponentInternalInstance,
22+
EmitFn,
23+
EmitsOptions,
24+
Slots,
25+
} from '../runtimeContext'
1426

1527
export type ComponentInstance = InstanceType<VueConstructor>
1628

@@ -79,3 +91,42 @@ export type VueProxy<
7991
ExtractPropTypes<PropsOptions>
8092
> &
8193
VueConstructorProxy<PropsOptions, RawBindings, Data, Computed, Methods>
94+
95+
// public properties exposed on the proxy, which is used as the render context
96+
// in templates (as `this` in the render option)
97+
export type ComponentPublicInstance<
98+
P = {}, // props type extracted from props option
99+
B = {}, // raw bindings returned from setup()
100+
D = {}, // return from data()
101+
C extends ComputedOptions = {},
102+
M extends MethodOptions = {},
103+
E extends EmitsOptions = {},
104+
PublicProps = P,
105+
Defaults = {},
106+
MakeDefaultsOptional extends boolean = false
107+
> = {
108+
$: ComponentInternalInstance
109+
$data: D
110+
$props: MakeDefaultsOptional extends true
111+
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
112+
: P & PublicProps
113+
$attrs: Data
114+
$refs: Data
115+
$slots: Slots
116+
$root: ComponentPublicInstance | null
117+
$parent: ComponentPublicInstance | null
118+
$emit: EmitFn<E>
119+
$el: any
120+
// $options: Options & MergedComponentOptionsOverride
121+
$forceUpdate: () => void
122+
$nextTick: typeof nextTick
123+
$watch(
124+
source: string | Function,
125+
cb: Function,
126+
options?: WatchOptions
127+
): WatchStopHandle
128+
} & P &
129+
ShallowUnwrapRef<B> &
130+
UnwrapNestedRefs<D> &
131+
ExtractComputedReturns<C> &
132+
M

src/component/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ export {
66
MethodOptions,
77
ComponentPropsOptions,
88
} from './componentOptions'
9-
export { ComponentInstance, ComponentRenderProxy } from './componentProxy'
9+
export {
10+
ComponentInstance,
11+
ComponentPublicInstance,
12+
ComponentRenderProxy,
13+
} from './componentProxy'
1014
export { Data } from './common'
1115
export {
1216
PropType,

src/runtimeContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ export interface SetupContext<E = EmitsOptions> {
166166
readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] }
167167
}
168168

169+
export interface ComponentPublicInstance {}
170+
169171
/**
170172
* We expose a subset of properties on the internal instance as they are
171173
* useful for advanced external libraries and tools.

0 commit comments

Comments
 (0)