| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import {
- ComputedOptions,
- MethodOptions,
- ComponentOptionsWithoutProps,
- ComponentOptionsWithArrayProps,
- ComponentOptionsWithObjectProps,
- ComponentOptionsMixin,
- RenderFunction,
- ComponentOptionsBase,
- ComponentInjectOptions,
- ComponentOptions
- } from './componentOptions'
- import {
- SetupContext,
- AllowedComponentProps,
- ComponentCustomProps
- } from './component'
- import {
- ExtractPropTypes,
- ComponentPropsOptions,
- ExtractDefaultPropTypes,
- ComponentObjectPropsOptions
- } from './componentProps'
- import { EmitsOptions, EmitsToProps } from './componentEmits'
- import { extend, isFunction } from '@vue/shared'
- import { VNodeProps } from './vnode'
- import {
- CreateComponentPublicInstance,
- ComponentPublicInstanceConstructor
- } from './componentPublicInstance'
- import { SlotsType } from './componentSlots'
- export type PublicProps = VNodeProps &
- AllowedComponentProps &
- ComponentCustomProps
- 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,
- S extends SlotsType = {},
- PP = PublicProps,
- Props = Readonly<
- PropsOrPropOptions extends ComponentPropsOptions
- ? ExtractPropTypes<PropsOrPropOptions>
- : PropsOrPropOptions
- > &
- ({} extends E ? {} : EmitsToProps<E>),
- Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
- > = ComponentPublicInstanceConstructor<
- CreateComponentPublicInstance<
- Props,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- S,
- PP & Props,
- Defaults,
- true
- > &
- Props
- > &
- ComponentOptionsBase<
- Props,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- EE,
- S,
- Defaults
- > &
- PP
- // 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
- }
- ): (props: Props & EmitsToProps<E>) => any
- 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
- }
- ): (props: Props & EmitsToProps<E>) => any
- // overload 2: object format with no props
- // (uses user defined props interface)
- // return type is for Vetur and TSX support
- export function defineComponent<
- Props = {},
- RawBindings = {},
- D = {},
- C extends ComputedOptions = {},
- M extends MethodOptions = {},
- Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
- Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
- E extends EmitsOptions = {},
- EE extends string = string,
- S extends SlotsType = {},
- I extends ComponentInjectOptions = {},
- II extends string = string
- >(
- options: ComponentOptionsWithoutProps<
- Props,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- EE,
- S,
- I,
- II
- >
- ): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE, S>
- // overload 3: object format with array props declaration
- // props inferred as { [key in PropNames]?: any }
- // return type is for Vetur and TSX support
- export function defineComponent<
- PropNames extends string,
- RawBindings,
- D,
- C extends ComputedOptions = {},
- M extends MethodOptions = {},
- Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
- Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
- E extends EmitsOptions = {},
- EE extends string = string,
- S extends SlotsType = {},
- I extends ComponentInjectOptions = {},
- II extends string = string
- >(
- options: ComponentOptionsWithArrayProps<
- PropNames,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- EE,
- S,
- I,
- II
- >
- ): DefineComponent<
- Readonly<{ [key in PropNames]?: any }>,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- EE,
- S
- >
- // overload 4: object format with object props declaration
- // see `ExtractPropTypes` in ./componentProps.ts
- export function defineComponent<
- // the Readonly constraint allows TS to treat the type of { required: true }
- // as constant instead of boolean.
- PropsOptions extends Readonly<ComponentPropsOptions>,
- RawBindings,
- D,
- C extends ComputedOptions = {},
- M extends MethodOptions = {},
- Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
- Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
- E extends EmitsOptions = {},
- EE extends string = string,
- S extends SlotsType = {},
- I extends ComponentInjectOptions = {},
- II extends string = string
- >(
- options: ComponentOptionsWithObjectProps<
- PropsOptions,
- RawBindings,
- D,
- C,
- M,
- Mixin,
- Extends,
- E,
- EE,
- S,
- I,
- II
- >
- ): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE, S>
- // implementation, close to no-op
- export function defineComponent(
- options: unknown,
- extraOptions?: ComponentOptions
- ) {
- return isFunction(options)
- ? extend({}, extraOptions, { setup: options, name: options.name })
- : options
- }
|