Skip to content

Commit 66b43eb

Browse files
committed
refactor: avoid redeclaring component public instance types
1 parent 72347b7 commit 66b43eb

File tree

2 files changed

+84
-51
lines changed

2 files changed

+84
-51
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export { Options, createDecorator, mixins, setup } from './helpers'
1010
* Other types
1111
*/
1212

13-
export { VueBase, VueMixin, VueStatic } from './vue'
13+
export { VueBase, VueMixin, VueStatic, VueConstructor } from './vue'
1414

1515
export {
1616
VueDecorator,

src/vue.ts

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,89 @@ function getSuperOptions(Ctor: Function): ComponentOptions | undefined {
3939
return Super.__vccOpts
4040
}
4141

42-
export interface ClassComponentHooks {
43-
/* To be extended on user land */
44-
}
42+
export interface VueStatic {
43+
// -- Class component configs
44+
45+
/** @internal */
46+
__vccCache?: ComponentOptions
47+
48+
/** @internal */
49+
__vccBase?: ComponentOptions
50+
51+
/** @internal */
52+
__vccDecorators?: ((options: ComponentOptions) => void)[]
53+
54+
/** @internal */
55+
__vccMixins?: ComponentOptions[]
56+
57+
/** @internal */
58+
__vccHooks: string[]
59+
60+
/** @internal */
61+
__vccOpts: ComponentOptions
62+
63+
// --- Vue Loader injections
64+
65+
/** @internal */
66+
render?: () => VNode | void
67+
68+
/** @internal */
69+
__file?: string
70+
71+
/** @internal */
72+
__cssModules?: Record<string, any>
73+
74+
/** @internal */
75+
__scopeId?: string
76+
77+
/** @internal */
78+
__hmrId?: string
79+
80+
// --- Public APIs
4581

46-
export type VueStatic = {
47-
[K in keyof typeof Vue]: typeof Vue[K]
82+
registerHooks(keys: string[]): void
4883
}
4984

5085
export type VueMixin<V extends Vue = Vue> = VueStatic & { prototype: V }
5186

5287
export type VueBase<V extends Vue = Vue> = VueMixin<V> &
5388
(new (...args: any[]) => V)
5489

55-
export class Vue<Props = unknown>
56-
implements
57-
ComponentPublicInstance<{}, {}, {}, {}, {}, {}, Props>,
58-
ClassComponentHooks {
90+
export interface ClassComponentHooks {
91+
// To be extended on user land
92+
93+
data?(): object
94+
beforeCreate?(): void
95+
created?(): void
96+
beforeMount?(): void
97+
mounted?(): void
98+
beforeUnmount?(): void
99+
unmounted?(): void
100+
beforeUpdate?(): void
101+
updated?(): void
102+
activated?(): void
103+
deactivated?(): void
104+
render?(): VNode | void
105+
errorCaptured?(err: Error, vm: Vue, info: string): boolean | undefined
106+
serverPrefetch?(): Promise<unknown>
107+
}
108+
109+
export type Vue<Props = unknown> = ComponentPublicInstance<
110+
{},
111+
{},
112+
{},
113+
{},
114+
{},
115+
{},
116+
Props
117+
> &
118+
ClassComponentHooks
119+
120+
export interface VueConstructor extends VueStatic {
121+
new <Props = unknown>(prop: Props, ctx: SetupContext): Vue<Props>
122+
}
123+
124+
class VueImpl {
59125
/** @internal */
60126
static __vccCache?: ComponentOptions
61127

@@ -147,7 +213,7 @@ export class Vue<Props = unknown>
147213
}
148214
})
149215

150-
options.setup = function (props: unknown, ctx: SetupContext) {
216+
options.setup = function (props: Record<string, any>, ctx: SetupContext) {
151217
const data: any = new Ctor(props, ctx)
152218
const dataKeys = Object.keys(data)
153219

@@ -188,10 +254,10 @@ export class Vue<Props = unknown>
188254
'__cssModules',
189255
'__scopeId',
190256
'__hmrId',
191-
] as const
257+
]
192258
injections.forEach((key) => {
193-
if (Ctor[key]) {
194-
options[key] = Ctor[key]
259+
if ((Ctor as any)[key]) {
260+
options[key] = (Ctor as any)[key]
195261
}
196262
})
197263

@@ -202,47 +268,12 @@ export class Vue<Props = unknown>
202268
this.__vccHooks.push(...keys)
203269
}
204270

205-
// Public instance properties
206-
$!: ComponentPublicInstance['$']
207-
$data!: ComponentPublicInstance['$data']
208-
$refs!: ComponentPublicInstance['$refs']
209-
$root!: ComponentPublicInstance['$root']
210-
$parent!: ComponentPublicInstance['$parent']
211-
$el!: ComponentPublicInstance['$el']
212-
$options!: ComponentPublicInstance['$options']
213-
$forceUpdate!: ComponentPublicInstance['$forceUpdate']
214-
$nextTick!: ComponentPublicInstance['$nextTick']
215-
$watch!: ComponentPublicInstance['$watch']
216-
217-
$props!: Props
271+
$props!: Record<string, any>
218272
$emit!: (event: string, ...args: any[]) => void
219273
$attrs!: ComponentPublicInstance['$attrs']
220274
$slots!: ComponentPublicInstance['$slots']
221275

222-
// Built-in hooks
223-
data?(): object
224-
beforeCreate?(): void
225-
created?(): void
226-
beforeMount?(): void
227-
mounted?(): void
228-
beforeUnmount?(): void
229-
unmounted?(): void
230-
beforeUpdate?(): void
231-
updated?(): void
232-
activated?(): void
233-
deactivated?(): void
234-
render?(): VNode | void
235-
errorCaptured?(err: Error, vm: Vue, info: string): boolean | undefined
236-
serverPrefetch?(): Promise<unknown>
237-
238-
// Vue Loader injections
239-
static render?: () => VNode | void
240-
static __file?: string
241-
static __cssModules?: Record<string, any>
242-
static __scopeId?: string
243-
static __hmrId?: string
244-
245-
constructor(props: Props, ctx: SetupContext) {
276+
constructor(props: Record<string, any>, ctx: SetupContext) {
246277
defineGetter(this, '$props', () => props)
247278
defineGetter(this, '$attrs', () => ctx.attrs)
248279
defineGetter(this, '$slots', () => ctx.slots)
@@ -258,3 +289,5 @@ export class Vue<Props = unknown>
258289
})
259290
}
260291
}
292+
293+
export const Vue: VueConstructor = VueImpl as VueConstructor

0 commit comments

Comments
 (0)