-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
apiDefineComponent.ts
315 lines (306 loc) · 7.96 KB
/
apiDefineComponent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import type {
ComponentInjectOptions,
ComponentOptions,
ComponentOptionsBase,
ComponentOptionsMixin,
ComponentProvideOptions,
ComputedOptions,
MethodOptions,
RenderFunction,
} from './componentOptions'
import type {
AllowedComponentProps,
Component,
ComponentCustomProps,
GlobalComponents,
GlobalDirectives,
SetupContext,
} from './component'
import type {
ComponentObjectPropsOptions,
ComponentPropsOptions,
ExtractDefaultPropTypes,
ExtractPropTypes,
} from './componentProps'
import type {
EmitsOptions,
EmitsToProps,
TypeEmitsToOptions,
} from './componentEmits'
import { type IsKeyValues, extend, isFunction } from '@vue/shared'
import type { VNodeProps } from './vnode'
import type {
ComponentPublicInstanceConstructor,
CreateComponentPublicInstanceWithMixins,
} from './componentPublicInstance'
import type { SlotsType } from './componentSlots'
import type { Directive } from './directives'
import type { ComponentTypeEmits } from './apiSetupHelpers'
export type PublicProps = VNodeProps &
AllowedComponentProps &
ComponentCustomProps
type ResolveProps<PropsOrPropOptions, E extends EmitsOptions> = Readonly<
PropsOrPropOptions extends ComponentPropsOptions
? ExtractPropTypes<PropsOrPropOptions>
: PropsOrPropOptions
> &
({} extends E ? {} : EmitsToProps<E>)
export type DefineComponent<
PropsOrPropOptions = {},
RawBindings = {},
D = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string,
PP = PublicProps,
Props = ResolveProps<PropsOrPropOptions, E>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
S extends SlotsType = {},
LC extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
MakeDefaultsOptional extends boolean = true,
TypeRefs extends Record<string, unknown> = {},
TypeEl extends Element = any,
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstanceWithMixins<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP,
Defaults,
MakeDefaultsOptional,
{},
S,
LC & GlobalComponents,
Directives & GlobalDirectives,
Exposed,
TypeRefs,
TypeEl
>
> &
ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Defaults,
{},
string,
S,
LC & GlobalComponents,
Directives & GlobalDirectives,
Exposed,
Provide
> &
PP
export type DefineSetupFnComponent<
P extends Record<string, any>,
E extends EmitsOptions = {},
S extends SlotsType = SlotsType,
Props = P & EmitsToProps<E>,
PP = PublicProps,
> = new (
props: Props & PP,
) => CreateComponentPublicInstanceWithMixins<
Props,
{},
{},
{},
{},
ComponentOptionsMixin,
ComponentOptionsMixin,
E,
PP,
{},
false,
{},
S
>
type ToResolvedProps<Props, Emits extends EmitsOptions> = Readonly<Props> &
Readonly<EmitsToProps<Emits>>
// defineComponent is a utility that is primarily used for type inference
// when declaring components. Type inference is provided in the component
// options (provided as the argument). The returned value has artificial types
// for TSX / manual render function / IDE support.
// overload 1: direct setup function
// (uses user defined props interface)
export function defineComponent<
Props extends Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
>(
setup: (
props: Props,
ctx: SetupContext<E, S>,
) => RenderFunction | Promise<RenderFunction>,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
props?: (keyof Props)[]
emits?: E | EE[]
slots?: S
},
): DefineSetupFnComponent<Props, E, S>
export function defineComponent<
Props extends Record<string, any>,
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
>(
setup: (
props: Props,
ctx: SetupContext<E, S>,
) => RenderFunction | Promise<RenderFunction>,
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
props?: ComponentObjectPropsOptions<Props>
emits?: E | EE[]
slots?: S
},
): DefineSetupFnComponent<Props, E, S>
// overload 2: defineComponent with options object, infer props from options
export function defineComponent<
// props
TypeProps,
RuntimePropsOptions extends
ComponentObjectPropsOptions = ComponentObjectPropsOptions,
RuntimePropsKeys extends string = string,
// emits
TypeEmits extends ComponentTypeEmits = {},
RuntimeEmitsOptions extends EmitsOptions = {},
RuntimeEmitsKeys extends string = string,
// other options
Data = {},
SetupBindings = {},
Computed extends ComputedOptions = {},
Methods extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
InjectOptions extends ComponentInjectOptions = {},
InjectKeys extends string = string,
Slots extends SlotsType = {},
LocalComponents extends Record<string, Component> = {},
Directives extends Record<string, Directive> = {},
Exposed extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
// resolved types
ResolvedEmits extends EmitsOptions = {} extends RuntimeEmitsOptions
? TypeEmitsToOptions<TypeEmits>
: RuntimeEmitsOptions,
InferredProps = IsKeyValues<TypeProps> extends true
? TypeProps
: string extends RuntimePropsKeys
? ComponentObjectPropsOptions extends RuntimePropsOptions
? {}
: ExtractPropTypes<RuntimePropsOptions>
: { [key in RuntimePropsKeys]?: any },
TypeRefs extends Record<string, unknown> = {},
TypeEl extends Element = any,
>(
options: {
props?: (RuntimePropsOptions & ThisType<void>) | RuntimePropsKeys[]
/**
* @private for language-tools use only
*/
__typeProps?: TypeProps
/**
* @private for language-tools use only
*/
__typeEmits?: TypeEmits
/**
* @private for language-tools use only
*/
__typeRefs?: TypeRefs
/**
* @private for language-tools use only
*/
__typeEl?: TypeEl
} & ComponentOptionsBase<
ToResolvedProps<InferredProps, ResolvedEmits>,
SetupBindings,
Data,
Computed,
Methods,
Mixin,
Extends,
RuntimeEmitsOptions,
RuntimeEmitsKeys,
{}, // Defaults
InjectOptions,
InjectKeys,
Slots,
LocalComponents,
Directives,
Exposed,
Provide
> &
ThisType<
CreateComponentPublicInstanceWithMixins<
ToResolvedProps<InferredProps, ResolvedEmits>,
SetupBindings,
Data,
Computed,
Methods,
Mixin,
Extends,
ResolvedEmits,
{},
{},
false,
InjectOptions,
Slots,
LocalComponents,
Directives,
Exposed
>
>,
): DefineComponent<
InferredProps,
SetupBindings,
Data,
Computed,
Methods,
Mixin,
Extends,
ResolvedEmits,
RuntimeEmitsKeys,
PublicProps,
ToResolvedProps<InferredProps, ResolvedEmits>,
ExtractDefaultPropTypes<RuntimePropsOptions>,
Slots,
LocalComponents,
Directives,
Exposed,
Provide,
// MakeDefaultsOptional - if TypeProps is provided, set to false to use
// user props types verbatim
unknown extends TypeProps ? true : false,
TypeRefs,
TypeEl
>
// implementation, close to no-op
/*! #__NO_SIDE_EFFECTS__ */
export function defineComponent(
options: unknown,
extraOptions?: ComponentOptions,
) {
return isFunction(options)
? // #8236: extend call and options.name access are considered side-effects
// by Rollup, so we have to wrap it in a pure-annotated IIFE.
/*@__PURE__*/ (() =>
extend({ name: options.name }, extraOptions, { setup: options }))()
: options
}