2019-09-06 23:19:22 +08:00
|
|
|
import type {
|
|
|
|
ComponentInjectOptions,
|
|
|
|
ComponentOptions,
|
2022-11-08 14:09:53 +08:00
|
|
|
ComponentOptionsBase,
|
2019-09-06 23:19:22 +08:00
|
|
|
ComponentOptionsMixin,
|
2024-04-25 16:04:03 +08:00
|
|
|
ComponentProvideOptions,
|
2020-09-15 23:46:11 +08:00
|
|
|
ComputedOptions,
|
2019-09-06 23:19:22 +08:00
|
|
|
MethodOptions,
|
2020-09-14 23:28:56 +08:00
|
|
|
RenderFunction,
|
2020-04-04 07:08:17 +08:00
|
|
|
} from './componentOptions'
|
2020-07-20 02:09:37 +08:00
|
|
|
import type {
|
|
|
|
AllowedComponentProps,
|
2024-04-25 16:04:03 +08:00
|
|
|
Component,
|
2020-07-20 02:09:37 +08:00
|
|
|
ComponentCustomProps,
|
2024-04-25 16:04:03 +08:00
|
|
|
GlobalComponents,
|
|
|
|
GlobalDirectives,
|
2020-07-20 02:09:37 +08:00
|
|
|
SetupContext,
|
|
|
|
} from './component'
|
2020-09-16 22:09:35 +08:00
|
|
|
import type {
|
|
|
|
ComponentObjectPropsOptions,
|
|
|
|
ComponentPropsOptions,
|
2023-03-27 18:28:43 +08:00
|
|
|
ExtractDefaultPropTypes,
|
|
|
|
ExtractPropTypes,
|
2020-09-16 22:09:35 +08:00
|
|
|
} from './componentProps'
|
2024-04-27 11:48:37 +08:00
|
|
|
import type {
|
|
|
|
EmitsOptions,
|
|
|
|
EmitsToProps,
|
|
|
|
TypeEmitsToOptions,
|
|
|
|
} from './componentEmits'
|
2023-03-27 18:28:43 +08:00
|
|
|
import { extend, isFunction } from '@vue/shared'
|
2020-07-20 02:09:37 +08:00
|
|
|
import type { VNodeProps } from './vnode'
|
2020-09-15 23:46:11 +08:00
|
|
|
import type {
|
|
|
|
ComponentPublicInstanceConstructor,
|
2024-05-01 01:09:06 +08:00
|
|
|
CreateComponentPublicInstanceWithMixins,
|
2020-09-15 23:46:11 +08:00
|
|
|
} from './componentPublicInstance'
|
2023-04-03 16:49:16 +08:00
|
|
|
import type { SlotsType } from './componentSlots'
|
2024-04-25 16:04:03 +08:00
|
|
|
import type { Directive } from './directives'
|
2024-04-27 11:48:37 +08:00
|
|
|
import type { ComponentTypeEmits } from './apiSetupHelpers'
|
2020-09-15 23:46:11 +08:00
|
|
|
|
|
|
|
export type PublicProps = VNodeProps &
|
|
|
|
AllowedComponentProps &
|
|
|
|
ComponentCustomProps
|
|
|
|
|
2023-04-05 22:30:50 +08:00
|
|
|
type ResolveProps<PropsOrPropOptions, E extends EmitsOptions> = Readonly<
|
|
|
|
PropsOrPropOptions extends ComponentPropsOptions
|
|
|
|
? ExtractPropTypes<PropsOrPropOptions>
|
|
|
|
: PropsOrPropOptions
|
|
|
|
> &
|
|
|
|
({} extends E ? {} : EmitsToProps<E>)
|
|
|
|
|
2020-09-15 23:46:11 +08:00
|
|
|
export type DefineComponent<
|
2020-09-22 22:02:19 +08:00
|
|
|
PropsOrPropOptions = {},
|
|
|
|
RawBindings = {},
|
|
|
|
D = {},
|
2020-09-15 23:46:11 +08:00
|
|
|
C extends ComputedOptions = ComputedOptions,
|
|
|
|
M extends MethodOptions = MethodOptions,
|
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2021-09-02 21:53:57 +08:00
|
|
|
E extends EmitsOptions = {},
|
2020-09-15 23:46:11 +08:00
|
|
|
EE extends string = string,
|
|
|
|
PP = PublicProps,
|
2023-04-05 22:30:50 +08:00
|
|
|
Props = ResolveProps<PropsOrPropOptions, E>,
|
|
|
|
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
|
|
|
|
S extends SlotsType = {},
|
2024-04-25 16:04:03 +08:00
|
|
|
LC extends Record<string, Component> = {},
|
|
|
|
Directives extends Record<string, Directive> = {},
|
|
|
|
Exposed extends string = string,
|
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions,
|
2024-04-27 11:48:37 +08:00
|
|
|
MakeDefaultsOptional extends boolean = true,
|
2024-08-05 10:50:43 +08:00
|
|
|
TypeRefs extends Record<string, unknown> = {},
|
2022-05-21 00:53:46 +08:00
|
|
|
> = ComponentPublicInstanceConstructor<
|
2024-05-01 01:09:06 +08:00
|
|
|
CreateComponentPublicInstanceWithMixins<
|
2022-05-21 00:53:46 +08:00
|
|
|
Props,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
PP & Props,
|
|
|
|
Defaults,
|
2024-04-27 11:48:37 +08:00
|
|
|
MakeDefaultsOptional,
|
2023-04-06 11:12:49 +08:00
|
|
|
{},
|
2024-04-25 16:04:03 +08:00
|
|
|
S,
|
|
|
|
LC & GlobalComponents,
|
|
|
|
Directives & GlobalDirectives,
|
2024-08-05 10:50:43 +08:00
|
|
|
Exposed,
|
|
|
|
TypeRefs
|
2023-11-09 17:40:36 +08:00
|
|
|
>
|
2022-05-21 00:53:46 +08:00
|
|
|
> &
|
2020-09-15 23:46:11 +08:00
|
|
|
ComponentOptionsBase<
|
2020-09-16 22:09:35 +08:00
|
|
|
Props,
|
2020-09-15 23:46:11 +08:00
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
2020-09-16 22:09:35 +08:00
|
|
|
EE,
|
2023-04-06 11:12:49 +08:00
|
|
|
Defaults,
|
|
|
|
{},
|
|
|
|
string,
|
2024-04-25 16:04:03 +08:00
|
|
|
S,
|
|
|
|
LC & GlobalComponents,
|
|
|
|
Directives & GlobalDirectives,
|
|
|
|
Exposed,
|
|
|
|
Provide
|
2020-09-15 23:46:11 +08:00
|
|
|
> &
|
|
|
|
PP
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2024-02-25 23:38:39 +08:00
|
|
|
export type DefineSetupFnComponent<
|
2024-02-25 21:10:08 +08:00
|
|
|
P extends Record<string, any>,
|
|
|
|
E extends EmitsOptions = {},
|
|
|
|
S extends SlotsType = SlotsType,
|
|
|
|
Props = P & EmitsToProps<E>,
|
|
|
|
PP = PublicProps,
|
|
|
|
> = new (
|
|
|
|
props: Props & PP,
|
2024-05-01 01:09:06 +08:00
|
|
|
) => CreateComponentPublicInstanceWithMixins<
|
2024-02-25 21:10:08 +08:00
|
|
|
Props,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
ComponentOptionsMixin,
|
|
|
|
ComponentOptionsMixin,
|
|
|
|
E,
|
|
|
|
PP,
|
|
|
|
{},
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
S
|
|
|
|
>
|
|
|
|
|
2019-12-22 23:58:12 +08:00
|
|
|
// defineComponent is a utility that is primarily used for type inference
|
2019-11-21 23:21:09 +08:00
|
|
|
// when declaring components. Type inference is provided in the component
|
2020-05-01 21:42:58 +08:00
|
|
|
// options (provided as the argument). The returned value has artificial types
|
2019-11-21 23:21:09 +08:00
|
|
|
// for TSX / manual render function / IDE support.
|
|
|
|
|
2019-09-06 23:19:22 +08:00
|
|
|
// overload 1: direct setup function
|
|
|
|
// (uses user defined props interface)
|
2023-03-27 18:28:43 +08:00
|
|
|
export function defineComponent<
|
|
|
|
Props extends Record<string, any>,
|
|
|
|
E extends EmitsOptions = {},
|
2023-04-03 16:49:16 +08:00
|
|
|
EE extends string = string,
|
|
|
|
S extends SlotsType = {},
|
2023-03-27 18:28:43 +08:00
|
|
|
>(
|
|
|
|
setup: (
|
|
|
|
props: Props,
|
2023-04-03 16:49:16 +08:00
|
|
|
ctx: SetupContext<E, S>,
|
2023-03-27 18:28:43 +08:00
|
|
|
) => RenderFunction | Promise<RenderFunction>,
|
|
|
|
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
|
|
|
|
props?: (keyof Props)[]
|
|
|
|
emits?: E | EE[]
|
2023-04-03 16:49:16 +08:00
|
|
|
slots?: S
|
2023-03-27 18:28:43 +08:00
|
|
|
},
|
2024-02-25 23:38:39 +08:00
|
|
|
): DefineSetupFnComponent<Props, E, S>
|
2023-03-27 18:28:43 +08:00
|
|
|
export function defineComponent<
|
|
|
|
Props extends Record<string, any>,
|
|
|
|
E extends EmitsOptions = {},
|
2023-04-03 16:49:16 +08:00
|
|
|
EE extends string = string,
|
|
|
|
S extends SlotsType = {},
|
2023-03-27 18:28:43 +08:00
|
|
|
>(
|
2019-11-10 07:40:25 +08:00
|
|
|
setup: (
|
2023-03-27 18:28:43 +08:00
|
|
|
props: Props,
|
2023-04-03 16:49:16 +08:00
|
|
|
ctx: SetupContext<E, S>,
|
2023-03-27 18:28:43 +08:00
|
|
|
) => RenderFunction | Promise<RenderFunction>,
|
|
|
|
options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
|
|
|
|
props?: ComponentObjectPropsOptions<Props>
|
|
|
|
emits?: E | EE[]
|
2023-04-03 16:49:16 +08:00
|
|
|
slots?: S
|
2023-03-27 18:28:43 +08:00
|
|
|
},
|
2024-02-25 23:38:39 +08:00
|
|
|
): DefineSetupFnComponent<Props, E, S>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
2024-04-27 11:48:37 +08:00
|
|
|
// overload 2: defineComponent with options object, infer props from options
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent<
|
2024-04-27 11:48:37 +08:00
|
|
|
// 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 = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2024-04-27 11:48:37 +08:00
|
|
|
InjectOptions extends ComponentInjectOptions = {},
|
|
|
|
InjectKeys extends string = string,
|
|
|
|
Slots extends SlotsType = {},
|
|
|
|
LocalComponents extends Record<string, Component> = {},
|
2024-04-25 16:04:03 +08:00
|
|
|
Directives extends Record<string, Directive> = {},
|
|
|
|
Exposed extends string = string,
|
|
|
|
Provide extends ComponentProvideOptions = ComponentProvideOptions,
|
2024-04-27 11:48:37 +08:00
|
|
|
// resolved types
|
|
|
|
ResolvedEmits extends EmitsOptions = {} extends RuntimeEmitsOptions
|
|
|
|
? TypeEmitsToOptions<TypeEmits>
|
|
|
|
: RuntimeEmitsOptions,
|
|
|
|
InferredProps = unknown extends TypeProps
|
|
|
|
? string extends RuntimePropsKeys
|
|
|
|
? ComponentObjectPropsOptions extends RuntimePropsOptions
|
|
|
|
? {}
|
|
|
|
: ExtractPropTypes<RuntimePropsOptions>
|
|
|
|
: { [key in RuntimePropsKeys]?: any }
|
|
|
|
: TypeProps,
|
|
|
|
ResolvedProps = Readonly<InferredProps & EmitsToProps<ResolvedEmits>>,
|
2024-08-05 10:50:43 +08:00
|
|
|
TypeRefs extends Record<string, unknown> = {},
|
2022-05-21 00:53:46 +08:00
|
|
|
>(
|
2024-04-27 11:48:37 +08:00
|
|
|
options: {
|
|
|
|
props?: (RuntimePropsOptions & ThisType<void>) | RuntimePropsKeys[]
|
|
|
|
/**
|
|
|
|
* @private for language-tools use only
|
|
|
|
*/
|
|
|
|
__typeProps?: TypeProps
|
|
|
|
/**
|
|
|
|
* @private for language-tools use only
|
|
|
|
*/
|
|
|
|
__typeEmits?: TypeEmits
|
2024-08-05 10:50:43 +08:00
|
|
|
/**
|
|
|
|
* @private for language-tools use only
|
|
|
|
*/
|
|
|
|
__typeRefs?: TypeRefs
|
2024-04-27 11:48:37 +08:00
|
|
|
} & ComponentOptionsBase<
|
|
|
|
ResolvedProps,
|
|
|
|
SetupBindings,
|
|
|
|
Data,
|
|
|
|
Computed,
|
|
|
|
Methods,
|
2022-05-19 07:34:35 +08:00
|
|
|
Mixin,
|
|
|
|
Extends,
|
2024-04-27 11:48:37 +08:00
|
|
|
RuntimeEmitsOptions,
|
|
|
|
RuntimeEmitsKeys,
|
|
|
|
{}, // Defaults
|
|
|
|
InjectOptions,
|
|
|
|
InjectKeys,
|
|
|
|
Slots,
|
|
|
|
LocalComponents,
|
2024-04-25 16:04:03 +08:00
|
|
|
Directives,
|
|
|
|
Exposed,
|
|
|
|
Provide
|
2024-04-27 11:48:37 +08:00
|
|
|
> &
|
|
|
|
ThisType<
|
2024-05-01 01:09:06 +08:00
|
|
|
CreateComponentPublicInstanceWithMixins<
|
2024-04-27 11:48:37 +08:00
|
|
|
ResolvedProps,
|
|
|
|
SetupBindings,
|
|
|
|
Data,
|
|
|
|
Computed,
|
|
|
|
Methods,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
ResolvedEmits,
|
|
|
|
RuntimeEmitsKeys,
|
|
|
|
{},
|
|
|
|
false,
|
|
|
|
InjectOptions,
|
|
|
|
Slots,
|
|
|
|
LocalComponents,
|
|
|
|
Directives,
|
|
|
|
Exposed
|
|
|
|
>
|
|
|
|
>,
|
2023-04-05 22:30:50 +08:00
|
|
|
): DefineComponent<
|
2024-04-27 11:48:37 +08:00
|
|
|
InferredProps,
|
|
|
|
SetupBindings,
|
|
|
|
Data,
|
|
|
|
Computed,
|
|
|
|
Methods,
|
2023-04-05 22:30:50 +08:00
|
|
|
Mixin,
|
|
|
|
Extends,
|
2024-04-27 11:48:37 +08:00
|
|
|
ResolvedEmits,
|
|
|
|
RuntimeEmitsKeys,
|
2023-04-05 22:30:50 +08:00
|
|
|
PublicProps,
|
2024-04-27 11:48:37 +08:00
|
|
|
ResolvedProps,
|
|
|
|
ExtractDefaultPropTypes<RuntimePropsOptions>,
|
|
|
|
Slots,
|
|
|
|
LocalComponents,
|
2024-04-25 16:04:03 +08:00
|
|
|
Directives,
|
|
|
|
Exposed,
|
2024-04-27 11:48:37 +08:00
|
|
|
Provide,
|
|
|
|
// MakeDefaultsOptional - if TypeProps is provided, set to false to use
|
|
|
|
// user props types verbatim
|
2024-08-05 10:50:43 +08:00
|
|
|
unknown extends TypeProps ? true : false,
|
|
|
|
TypeRefs
|
2023-04-05 22:30:50 +08:00
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
// implementation, close to no-op
|
2023-07-11 17:52:43 +08:00
|
|
|
/*! #__NO_SIDE_EFFECTS__ */
|
2023-03-27 18:28:43 +08:00
|
|
|
export function defineComponent(
|
|
|
|
options: unknown,
|
|
|
|
extraOptions?: ComponentOptions,
|
|
|
|
) {
|
|
|
|
return isFunction(options)
|
2023-05-08 12:12:15 +08:00
|
|
|
? // #8326: 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 }))()
|
2023-03-27 18:28:43 +08:00
|
|
|
: options
|
2019-09-06 23:19:22 +08:00
|
|
|
}
|