Skip to content

Commit f8de4ca

Browse files
committed
fix(types): fix missing instance properties on defineComponent this
ref #12628 (comment)
1 parent d3add06 commit f8de4ca

File tree

3 files changed

+51
-42
lines changed

3 files changed

+51
-42
lines changed

types/test/v3/define-component-test.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -1139,9 +1139,14 @@ defineComponent({
11391139
}
11401140
})
11411141

1142+
// Missing / mismatching Vue 2 properties
11421143
// https://github.com/vuejs/vue/issues/12628#issuecomment-1177258223
11431144
defineComponent({
11441145
render(h) {
1146+
this.$listeners
1147+
this.$on('foo', () => {})
1148+
this.$ssrContext
1149+
this.$isServer
11451150
return h('div', {}, [...this.$slots.default!])
11461151
}
11471152
})

types/v3-component-public-instance.d.ts

+34-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { ExtractDefaultPropTypes, ExtractPropTypes } from './v3-component-props'
21
import {
32
DebuggerEvent,
4-
nextTick,
53
ShallowUnwrapRef,
6-
UnwrapNestedRefs,
7-
WatchOptions,
8-
WatchStopHandle
4+
UnwrapNestedRefs
95
} from './v3-generated'
10-
import { Data, UnionToIntersection } from './common'
6+
import { UnionToIntersection } from './common'
117

12-
import { VueConstructor } from './vue'
8+
import { Vue, Vue2Instance, VueConstructor } from './vue'
139
import {
1410
ComputedOptions,
1511
MethodOptions,
@@ -153,37 +149,43 @@ export type ComponentPublicInstance<
153149
any,
154150
any
155151
>
156-
> = {
157-
// $: ComponentInternalInstance
158-
$data: D
159-
$props: Readonly<
160-
MakeDefaultsOptional extends true
161-
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
162-
: P & PublicProps
163-
>
164-
$attrs: Data
165-
$refs: Data
166-
$slots: Record<string, VNode[] | undefined>
167-
$scopedSlots: Slots
168-
$root: ComponentPublicInstance | null
169-
$parent: ComponentPublicInstance | null
170-
$emit: EmitFn<E>
171-
$el: any
172-
$options: Options & MergedComponentOptionsOverride
173-
$forceUpdate: () => void
174-
$nextTick: typeof nextTick
175-
$watch(
176-
source: string | Function,
177-
cb: Function,
178-
options?: WatchOptions
179-
): WatchStopHandle
180-
} & Readonly<P> &
152+
> = Vue3Instance<
153+
D,
154+
P,
155+
PublicProps,
156+
E,
157+
Defaults,
158+
MakeDefaultsOptional,
159+
Options
160+
> &
161+
Readonly<P> &
181162
ShallowUnwrapRef<B> &
182163
UnwrapNestedRefs<D> &
183164
ExtractComputedReturns<C> &
184165
M &
185166
ComponentCustomProperties
186167

168+
interface Vue3Instance<
169+
D,
170+
P,
171+
PublicProps,
172+
E,
173+
Defaults,
174+
MakeDefaultsOptional,
175+
Options
176+
> extends Vue2Instance {
177+
$data: D
178+
readonly $props: Readonly<
179+
MakeDefaultsOptional extends true
180+
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
181+
: P & PublicProps
182+
>
183+
readonly $root: ComponentPublicInstance | null
184+
readonly $parent: ComponentPublicInstance | null
185+
readonly $emit: EmitFn<E>
186+
readonly $options: Options & MergedComponentOptionsOverride
187+
}
188+
187189
type MergedHook<T = () => void> = T | T[]
188190

189191
export type MergedComponentOptionsOverride = {

types/vue.d.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import {
33
AsyncComponent,
44
ComponentOptions,
55
FunctionalComponentOptions,
6-
WatchOptionsWithHandler,
7-
WatchHandler,
86
DirectiveOptions,
97
DirectiveFunction,
108
RecordPropsDefinition,
@@ -15,6 +13,7 @@ import {
1513
import { VNode, VNodeData, VNodeChildren, NormalizedScopedSlot } from './vnode'
1614
import { PluginFunction, PluginObject } from './plugin'
1715
import { DefineComponent } from './v3-define-component'
16+
import { nextTick } from './v3-generated'
1817

1918
export interface CreateElement {
2019
(
@@ -36,20 +35,25 @@ export interface CreateElement {
3635
): VNode
3736
}
3837

39-
export interface Vue {
40-
readonly $el: Element
41-
readonly $options: ComponentOptions<Vue>
38+
export interface Vue extends Vue2Instance {
39+
readonly $data: Record<string, any>
40+
readonly $props: Record<string, any>
4241
readonly $parent: Vue
4342
readonly $root: Vue
4443
readonly $children: Vue[]
44+
readonly $options: ComponentOptions<Vue>
45+
$emit(event: string, ...args: any[]): this
46+
}
47+
48+
export interface Vue2Instance {
49+
readonly $el: Element
4550
readonly $refs: {
4651
[key: string]: Vue | Element | (Vue | Element)[] | undefined
4752
}
4853
readonly $slots: { [key: string]: VNode[] | undefined }
4954
readonly $scopedSlots: { [key: string]: NormalizedScopedSlot | undefined }
5055
readonly $isServer: boolean
51-
readonly $data: Record<string, any>
52-
readonly $props: Record<string, any>
56+
5357
readonly $ssrContext: any
5458
readonly $vnode: VNode
5559
readonly $attrs: Record<string, string>
@@ -73,9 +77,7 @@ export interface Vue {
7377
$on(event: string | string[], callback: Function): this
7478
$once(event: string | string[], callback: Function): this
7579
$off(event?: string | string[], callback?: Function): this
76-
$emit(event: string, ...args: any[]): this
77-
$nextTick(callback: (this: this) => void): void
78-
$nextTick(): Promise<void>
80+
$nextTick: typeof nextTick
7981
$createElement: CreateElement
8082
}
8183

0 commit comments

Comments
 (0)