vue3-yuanma/packages/runtime-core/src/apiDefineComponent.ts

103 lines
2.8 KiB
TypeScript
Raw Normal View History

import {
ComputedOptions,
MethodOptions,
ComponentOptionsWithoutProps,
ComponentOptionsWithArrayProps,
2019-10-08 13:26:09 +00:00
ComponentOptionsWithObjectProps
} from './apiOptions'
2019-10-21 17:44:01 +00:00
import { SetupContext, RenderFunction } from './component'
import { ComponentPublicInstance } from './componentProxy'
import { ExtractPropTypes, ComponentPropsOptions } from './componentProps'
import { isFunction } from '@vue/shared'
2019-11-01 13:58:27 +00:00
import { VNodeProps } from './vnode'
// defineComponent is a utility that is primarily used for type inference
2019-11-21 15:21:09 +00:00
// when declaring components. Type inference is provided in the component
// options (provided as the argument). The returned value has artifical types
// for TSX / manual render function / IDE support.
// overload 1: direct setup function
// (uses user defined props interface)
export function defineComponent<Props, RawBindings = object>(
2019-11-09 23:40:25 +00:00
setup: (
props: Readonly<Props>,
ctx: SetupContext
) => RawBindings | RenderFunction
): {
new (): ComponentPublicInstance<
Props,
RawBindings,
{},
{},
{},
// public props
2019-11-01 13:58:27 +00:00
VNodeProps & Props
>
}
// 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 = {}
>(
options: ComponentOptionsWithoutProps<Props, RawBindings, D, C, M>
): {
new (): ComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
2019-11-01 13:58:27 +00:00
VNodeProps & Props
>
}
// 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 = {}
>(
options: ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M>
): {
// array props technically doesn't place any contraints on props in TSX
2019-11-01 13:58:27 +00:00
new (): ComponentPublicInstance<VNodeProps, RawBindings, D, C, M>
}
// 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 = {}
>(
2019-10-08 13:26:09 +00:00
options: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M>
): {
2019-09-06 16:58:31 +00:00
new (): ComponentPublicInstance<
ExtractPropTypes<PropsOptions>,
RawBindings,
D,
C,
M,
2019-11-01 13:58:27 +00:00
VNodeProps & ExtractPropTypes<PropsOptions, false>
>
}
// implementation, close to no-op
export function defineComponent(options: unknown) {
2019-10-05 14:09:34 +00:00
return isFunction(options) ? { setup: options } : options
}