Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(runtime-core): refactor defineComponent #1883

Merged
merged 28 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
19d37b7
types(runtime-core): move jsx defineComponent to jsx.d.ts
pikax Aug 18, 2020
a70b614
chore: improve return type for defineComponent
pikax Aug 18, 2020
084a090
chore: replace apiDefineComponet
pikax Aug 18, 2020
5d92fa1
chore: remove some old code
pikax Aug 18, 2020
d163f78
chore: working
pikax Aug 18, 2020
133d6b0
chore: more changes
pikax Aug 18, 2020
b07f54d
chore: reducing code and types
pikax Aug 18, 2020
d5968ea
wip
pikax Aug 18, 2020
cf809a4
chore: improvements
pikax Aug 18, 2020
0c7c24e
chore: working
pikax Aug 18, 2020
30c7351
chore: remove unnecessary code
pikax Aug 18, 2020
c2fd006
chore: remove commented code
pikax Aug 18, 2020
685ef7f
chore: readd h defineComponent overload
pikax Aug 18, 2020
3548497
chore: from jsx contructor
pikax Aug 18, 2020
d8e9a12
chore: remove unuse symbol
pikax Aug 20, 2020
e2b5697
Merge branch 'master-upstream' into types/move_jsx_types_to_jsx.d.ts
pikax Aug 20, 2020
dc9db23
chore: fix h overload
pikax Aug 20, 2020
f96a16d
chore: improve Component type and allow to infer Props/RawBinding
pikax Aug 20, 2020
a87e2e9
chore: fixed errors?
pikax Aug 20, 2020
114e067
chore: ...
pikax Aug 20, 2020
832bec6
chore: fixed?
pikax Aug 20, 2020
dac2393
chore: improve?
pikax Aug 20, 2020
ba11f2a
chore: remove commented code
pikax Aug 20, 2020
99b4bf0
chore: remove comment
pikax Aug 21, 2020
a669321
Merge branch 'master-upstream' into types/move_jsx_types_to_jsx.d.ts
pikax Aug 25, 2020
7fe7d3c
Merge branch 'master' into types/move_jsx_types_to_jsx.d.ts
pikax Aug 31, 2020
03dfaa5
refactor: improve typing
yyx990803 Sep 15, 2020
2e2daba
wip: Merge branch 'master' into types/move_jsx_types_to_jsx.d.ts
yyx990803 Sep 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 74 additions & 101 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,72 @@ import {
ComponentOptionsWithArrayProps,
ComponentOptionsWithObjectProps,
ComponentOptionsMixin,
RenderFunction
RenderFunction,
ComponentOptionsBase
} from './componentOptions'
import {
SetupContext,
FunctionalComponent,
AllowedComponentProps,
ComponentCustomProps
} from './component'
import {
CreateComponentPublicInstance,
ComponentPublicInstanceConstructor
} from './componentProxy'
import { ExtractPropTypes, ComponentPropsOptions } from './componentProps'
import { EmitsOptions } from './componentEmits'
import { isFunction } from '@vue/shared'
import { VNodeProps } from './vnode'
import {
CreateComponentPublicInstance,
ComponentPublicInstance
} from './componentProxy'

declare const JSX: unique symbol
pikax marked this conversation as resolved.
Show resolved Hide resolved
export interface DefineComponentJSX<
T extends ComponentPublicInstance = ComponentPublicInstance
> {
new (): T
}

export type PublicProps = VNodeProps &
AllowedComponentProps &
ComponentCustomProps

export type DefineComponent<
PropsOrPropOptions = any,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
PP = PublicProps,
RequiredProps = Readonly<ExtractPropTypes<PropsOrPropOptions>>,
OptionalProps = Readonly<ExtractPropTypes<PropsOrPropOptions, false>>
> = DefineComponentJSX<
CreateComponentPublicInstance<
OptionalProps,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP & OptionalProps
>
> &
ComponentOptionsBase<
RequiredProps,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
> &
PP

// defineComponent is a utility that is primarily used for type inference
// when declaring components. Type inference is provided in the component
Expand All @@ -34,21 +84,7 @@ export function defineComponent<Props, RawBindings = object>(
props: Readonly<Props>,
ctx: SetupContext
) => RawBindings | RenderFunction
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
{},
{},
{},
{},
{},
{},
// public props
VNodeProps & Props & AllowedComponentProps & ComponentCustomProps
>
> &
FunctionalComponent<Props>
): DefineComponent<Props, RawBindings>

// overload 2: object format with no props
// (uses user defined props interface)
Expand All @@ -57,11 +93,11 @@ export function defineComponent<
Props = {},
RawBindings = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
E extends EmitsOptions = EmitsOptions,
EE extends string = string
>(
options: ComponentOptionsWithoutProps<
Expand All @@ -75,30 +111,7 @@ export function defineComponent<
E,
EE
>
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
VNodeProps & Props & AllowedComponentProps & ComponentCustomProps
>
> &
ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>

// overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: any }
Expand All @@ -125,32 +138,17 @@ export function defineComponent<
E,
EE
>
): ComponentPublicInstanceConstructor<
// array props technically doesn't place any constraints on props in TSX before,
// but now we can export array props in TSX
CreateComponentPublicInstance<
Readonly<{ [key in PropNames]?: any }>,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
AllowedComponentProps & ComponentCustomProps
>
> &
ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<
Readonly<{ [key in PropNames]?: any }>,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>

// overload 4: object format with object props declaration
// see `ExtractPropTypes` in ./componentProps.ts
Expand Down Expand Up @@ -178,34 +176,9 @@ export function defineComponent<
E,
EE
>
): ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
ExtractPropTypes<PropsOptions, false>,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
VNodeProps & AllowedComponentProps & ComponentCustomProps
>
> &
ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>

// implementation, close to no-op
export function defineComponent(options: unknown) {
return isFunction(options)
? { setup: options, name: options.name }
: options
return isFunction(options) ? { setup: options, name: options.name } : options
}
2 changes: 1 addition & 1 deletion packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface ClassComponent {
__vccOpts: ComponentOptions
}

export type Component = ComponentOptions | FunctionalComponent<any>
export type Component = ComponentOptions | FunctionalComponent<any, any>

// A type used in public APIs where a component type is expected.
// The constructor type is an artificial type returned by defineComponent().
Expand Down
10 changes: 9 additions & 1 deletion packages/runtime-core/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RawSlots } from './componentSlots'
import { FunctionalComponent, Component } from './component'
import { ComponentOptions } from './componentOptions'
import { EmitsOptions } from './componentEmits'
import { DefineComponent } from './apiDefineComponent'

// `h` is a more user-friendly version of `createVNode` that allows omitting the
// props when possible. It is intended for manually written render functions.
Expand Down Expand Up @@ -50,7 +51,7 @@ type RawProps = VNodeProps & {
__v_isVNode?: never
// used to differ from Array children
[Symbol.iterator]?: never
} & { [key: string]: any }
} & Record<string, any>

type RawChildren =
| string
Expand Down Expand Up @@ -131,6 +132,13 @@ export function h<P>(
props?: (RawProps & P) | ({} extends P ? null : never),
children?: RawChildren | RawSlots
): VNode
// fake constructor type returned by `defineComponent`
export function h(type: DefineComponent, children?: RawChildren): VNode
export function h<P>(
type: DefineComponent<P>,
props: (RawProps & P) | ({} extends P ? null : never),
children?: RawChildren | RawSlots
): VNode

// Actual implementation
export function h(type: any, propsOrChildren?: any, children?: any): VNode {
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export {
} from './apiLifecycle'
export { provide, inject } from './apiInject'
export { nextTick } from './scheduler'
export { defineComponent } from './apiDefineComponent'
export {
defineComponent,
DefineComponent,
DefineComponentJSX
} from './apiDefineComponent'
export { defineAsyncComponent } from './apiAsyncComponent'

// Advanced API ----------------------------------------------------------------
Expand Down Expand Up @@ -177,7 +181,8 @@ export {
} from './componentOptions'
export {
ComponentPublicInstance,
ComponentCustomProperties
ComponentCustomProperties,
CreateComponentPublicInstance
} from './componentProxy'
export {
Renderer,
Expand Down