-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathcomponent.ts
95 lines (84 loc) · 2.93 KB
/
component.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
import Vue, { VueConstructor, VNode, ComponentOptions as Vue2ComponentOptions } from 'vue';
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps';
import { UnwrapRef } from '../reactivity';
import { HasDefined } from '../types/basic';
export type Data = { [key: string]: unknown };
export type ComponentInstance = InstanceType<VueConstructor>;
// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = {
$data: S;
$props: PublicProps;
$attrs: Data;
$refs: Data;
$slots: Data;
$root: ComponentInstance | null;
$parent: ComponentInstance | null;
$emit: (event: string, ...args: unknown[]) => void;
} & P &
S;
// for Vetur and TSX support
type VueConstructorProxy<PropsOptions, RawBindings> = {
new (): ComponentRenderProxy<
ExtractPropTypes<PropsOptions>,
UnwrapRef<RawBindings>,
ExtractPropTypes<PropsOptions, false>
>;
};
type VueProxy<PropsOptions, RawBindings> = Vue2ComponentOptions<
Vue,
UnwrapRef<RawBindings>,
never,
never,
PropsOptions,
ExtractPropTypes<PropsOptions, false>
> &
VueConstructorProxy<PropsOptions, RawBindings>;
export interface SetupContext {
readonly attrs: Record<string, string>;
readonly slots: { [key: string]: (...args: any[]) => VNode[] };
readonly parent: ComponentInstance | null;
readonly root: ComponentInstance;
readonly listeners: { [key: string]: Function };
emit(event: string, ...args: any[]): void;
}
export type SetupFunction<Props, RawBindings> = (
this: void,
props: Props,
ctx: SetupContext
) => RawBindings | (() => VNode | null);
interface ComponentOptionsWithProps<
PropsOptions = ComponentPropsOptions,
RawBindings = Data,
Props = ExtractPropTypes<PropsOptions>
> {
props?: PropsOptions;
setup?: SetupFunction<Props, RawBindings>;
}
interface ComponentOptionsWithoutProps<Props = never, RawBindings = Data> {
props?: undefined;
setup?: SetupFunction<Props, RawBindings>;
}
// overload 1: object format with no props
export function createComponent<RawBindings>(
options: ComponentOptionsWithoutProps<never, RawBindings>
): VueProxy<never, RawBindings>;
// overload 2: object format with object props declaration
// see `ExtractPropTypes` in ./componentProps.ts
export function createComponent<
Props,
RawBindings = Data,
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
>(
// prettier-ignore
options: (
// prefer the provided Props, otherwise infer it from PropsOptions
HasDefined<Props> extends true
? ComponentOptionsWithProps<PropsOptions, RawBindings, Props>
: ComponentOptionsWithProps<PropsOptions, RawBindings>) &
Omit<Vue2ComponentOptions<Vue>, keyof ComponentOptionsWithProps<never, never>>
): VueProxy<PropsOptions, RawBindings>;
// implementation, close to no-op
export function createComponent(options: any) {
return options as any;
}