2019-09-06 23:19:22 +08:00
|
|
|
import {
|
|
|
|
ComputedOptions,
|
|
|
|
MethodOptions,
|
|
|
|
ComponentOptionsWithoutProps,
|
|
|
|
ComponentOptionsWithArrayProps,
|
2020-05-01 22:37:40 +08:00
|
|
|
ComponentOptionsWithObjectProps,
|
2020-06-09 22:37:00 +08:00
|
|
|
ComponentOptionsMixin,
|
2020-09-14 23:28:56 +08:00
|
|
|
RenderFunction,
|
2020-09-15 23:46:11 +08:00
|
|
|
ComponentOptionsBase
|
2020-04-04 07:08:17 +08:00
|
|
|
} from './componentOptions'
|
2020-07-20 02:09:37 +08:00
|
|
|
import {
|
|
|
|
SetupContext,
|
|
|
|
AllowedComponentProps,
|
|
|
|
ComponentCustomProps
|
|
|
|
} from './component'
|
2020-09-16 22:09:35 +08:00
|
|
|
import {
|
|
|
|
ExtractPropTypes,
|
|
|
|
ComponentPropsOptions,
|
|
|
|
ExtractDefaultPropTypes
|
|
|
|
} from './componentProps'
|
2021-07-20 06:22:19 +08:00
|
|
|
import { EmitsOptions, EmitsToProps } from './componentEmits'
|
2019-09-06 23:19:22 +08:00
|
|
|
import { isFunction } from '@vue/shared'
|
2020-07-20 02:09:37 +08:00
|
|
|
import { VNodeProps } from './vnode'
|
2020-09-15 23:46:11 +08:00
|
|
|
import {
|
|
|
|
CreateComponentPublicInstance,
|
|
|
|
ComponentPublicInstanceConstructor
|
|
|
|
} from './componentPublicInstance'
|
|
|
|
|
|
|
|
export type PublicProps = VNodeProps &
|
|
|
|
AllowedComponentProps &
|
|
|
|
ComponentCustomProps
|
|
|
|
|
|
|
|
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,
|
2021-09-22 00:41:10 +08:00
|
|
|
Props = Readonly<
|
|
|
|
PropsOrPropOptions extends ComponentPropsOptions
|
|
|
|
? ExtractPropTypes<PropsOrPropOptions>
|
|
|
|
: PropsOrPropOptions
|
|
|
|
> &
|
2021-09-02 21:53:57 +08:00
|
|
|
({} extends E ? {} : EmitsToProps<E>),
|
2020-09-16 22:09:35 +08:00
|
|
|
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
|
2020-09-15 23:46:11 +08:00
|
|
|
> = ComponentPublicInstanceConstructor<
|
|
|
|
CreateComponentPublicInstance<
|
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
|
|
|
PP & Props,
|
|
|
|
Defaults,
|
|
|
|
true
|
2020-09-15 23:46:11 +08:00
|
|
|
> &
|
2020-09-16 22:09:35 +08:00
|
|
|
Props
|
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,
|
|
|
|
Defaults
|
2020-09-15 23:46:11 +08:00
|
|
|
> &
|
|
|
|
PP
|
2019-09-06 23:19:22 +08:00
|
|
|
|
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)
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent<Props, RawBindings = object>(
|
2019-11-10 07:40:25 +08:00
|
|
|
setup: (
|
|
|
|
props: Readonly<Props>,
|
|
|
|
ctx: SetupContext
|
|
|
|
) => RawBindings | RenderFunction
|
2020-09-15 23:46:11 +08:00
|
|
|
): DefineComponent<Props, RawBindings>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
// overload 2: object format with no props
|
|
|
|
// (uses user defined props interface)
|
|
|
|
// return type is for Vetur and TSX support
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent<
|
2020-04-04 00:05:52 +08:00
|
|
|
Props = {},
|
|
|
|
RawBindings = {},
|
|
|
|
D = {},
|
2020-09-18 12:13:47 +08:00
|
|
|
C extends ComputedOptions = {},
|
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-09-15 23:46:11 +08:00
|
|
|
E extends EmitsOptions = EmitsOptions,
|
2020-04-04 00:05:52 +08:00
|
|
|
EE extends string = string
|
2019-09-06 23:19:22 +08:00
|
|
|
>(
|
2020-06-09 22:37:00 +08:00
|
|
|
options: ComponentOptionsWithoutProps<
|
2021-08-10 00:36:34 +08:00
|
|
|
Props,
|
2019-11-01 00:43:05 +08:00
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE
|
|
|
|
>
|
2020-09-15 23:46:11 +08:00
|
|
|
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
// overload 3: object format with array props declaration
|
2019-11-01 00:43:05 +08:00
|
|
|
// props inferred as { [key in PropNames]?: any }
|
2019-09-06 23:19:22 +08:00
|
|
|
// return type is for Vetur and TSX support
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent<
|
2019-09-06 23:19:22 +08:00
|
|
|
PropNames extends string,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions = {},
|
2020-04-04 00:05:52 +08:00
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-04-04 00:05:52 +08:00
|
|
|
E extends EmitsOptions = Record<string, any>,
|
|
|
|
EE extends string = string
|
2019-09-06 23:19:22 +08:00
|
|
|
>(
|
2020-04-04 00:05:52 +08:00
|
|
|
options: ComponentOptionsWithArrayProps<
|
|
|
|
PropNames,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE
|
|
|
|
>
|
2020-09-15 23:46:11 +08:00
|
|
|
): DefineComponent<
|
|
|
|
Readonly<{ [key in PropNames]?: any }>,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
|
|
|
Mixin,
|
|
|
|
Extends,
|
|
|
|
E,
|
|
|
|
EE
|
|
|
|
>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
// overload 4: object format with object props declaration
|
|
|
|
// see `ExtractPropTypes` in ./componentProps.ts
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent<
|
2019-11-02 22:40:08 +08:00
|
|
|
// the Readonly constraint allows TS to treat the type of { required: true }
|
|
|
|
// as constant instead of boolean.
|
2019-11-02 05:06:19 +08:00
|
|
|
PropsOptions extends Readonly<ComponentPropsOptions>,
|
2019-09-06 23:19:22 +08:00
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C extends ComputedOptions = {},
|
2020-04-04 00:05:52 +08:00
|
|
|
M extends MethodOptions = {},
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
|
|
|
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
2020-04-04 00:05:52 +08:00
|
|
|
E extends EmitsOptions = Record<string, any>,
|
|
|
|
EE extends string = string
|
2019-09-06 23:19:22 +08:00
|
|
|
>(
|
2020-04-04 00:05:52 +08:00
|
|
|
options: ComponentOptionsWithObjectProps<
|
|
|
|
PropsOptions,
|
|
|
|
RawBindings,
|
|
|
|
D,
|
|
|
|
C,
|
|
|
|
M,
|
2020-06-09 22:37:00 +08:00
|
|
|
Mixin,
|
|
|
|
Extends,
|
2020-04-04 00:05:52 +08:00
|
|
|
E,
|
|
|
|
EE
|
|
|
|
>
|
2020-09-15 23:46:11 +08:00
|
|
|
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
|
2019-09-06 23:19:22 +08:00
|
|
|
|
|
|
|
// implementation, close to no-op
|
2019-12-22 23:58:12 +08:00
|
|
|
export function defineComponent(options: unknown) {
|
2020-08-20 04:11:29 +08:00
|
|
|
return isFunction(options) ? { setup: options, name: options.name } : options
|
2019-09-06 23:19:22 +08:00
|
|
|
}
|