Skip to content

Commit

Permalink
chore: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyxu committed Sep 1, 2023
1 parent b8d6793 commit df90c7c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
17 changes: 8 additions & 9 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
import {
SetupContext,
AllowedComponentProps,
ComponentCustomProps,
Data
ComponentCustomProps
} from './component'
import {
ExtractPropTypes,
Expand Down Expand Up @@ -59,7 +58,7 @@ export type DefineComponent<
Props = ResolveProps<PropsOrPropOptions, E>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
Expand Down Expand Up @@ -109,10 +108,10 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
PropsAttrs = IsSameType<Data, UnwrapAttrsType<Attrs>> extends true
Attrs extends AttrsType | undefined = undefined,
PropsAttrs = IsSameType<undefined, Attrs> extends true
? {}
: UnwrapAttrsType<Attrs>
: UnwrapAttrsType<NonNullable<Attrs>>
>(
setup: (
props: Props,
Expand Down Expand Up @@ -156,7 +155,7 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
I extends ComponentInjectOptions = {},
II extends string = string
>(
Expand Down Expand Up @@ -206,7 +205,7 @@ export function defineComponent<
E extends EmitsOptions = {},
EE extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<{ [key in PropNames]?: any }>
Expand Down Expand Up @@ -260,7 +259,7 @@ export function defineComponent<
S extends SlotsType = {},
I extends ComponentInjectOptions = {},
II extends string = string,
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand Down
9 changes: 6 additions & 3 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
exposeSetupStateOnRenderContext,
ComponentPublicInstanceConstructor,
publicPropertiesMap,
RuntimeCompiledPublicInstanceProxyHandlers
RuntimeCompiledPublicInstanceProxyHandlers,
IsSameType
} from './componentPublicInstance'
import {
ComponentPropsOptions,
Expand Down Expand Up @@ -187,10 +188,12 @@ type LifecycleHook<TFn = Function> = TFn[] | null
export type SetupContext<
E = EmitsOptions,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
> = E extends any
? {
attrs: Partial<UnwrapAttrsType<Attrs>>
attrs: IsSameType<Attrs, undefined> extends true
? Data
: UnwrapAttrsType<NonNullable<Attrs>>
slots: UnwrapSlotsType<S>
emit: EmitFn<E>
expose: (exposed?: Record<string, any>) => void
Expand Down
16 changes: 8 additions & 8 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface ComponentOptionsBase<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
ComponentInternalOptions,
ComponentCustomOptions {
Expand Down Expand Up @@ -226,7 +226,7 @@ export type ComponentOptionsWithoutProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
PE = Props & EmitsToProps<E>
> = ComponentOptionsBase<
PE,
Expand Down Expand Up @@ -277,7 +277,7 @@ export type ComponentOptionsWithArrayProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
Props = Prettify<Readonly<{ [key in PropNames]?: any } & EmitsToProps<E>>>
> = ComponentOptionsBase<
Props,
Expand Down Expand Up @@ -328,7 +328,7 @@ export type ComponentOptionsWithObjectProps<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
Props = Prettify<Readonly<ExtractPropTypes<PropsOptions> & EmitsToProps<E>>>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Expand Down Expand Up @@ -421,17 +421,17 @@ declare const AttrSymbol: unique symbol
export type AttrsType<T extends Record<string, any> = Record<string, any>> = {
[AttrSymbol]?: T
}

export type UnwrapAttrsType<
S extends AttrsType,
T = NonNullable<S[typeof AttrSymbol]>
> = [keyof S] extends [never]
Attrs extends AttrsType,
T = NonNullable<Attrs[typeof AttrSymbol]>
> = [keyof Attrs] extends [never]
? Data
: Readonly<
Prettify<{
[K in keyof T]: T[K]
}>
>

export type ComputedOptions = Record<
string,
ComputedGetter<any> | WritableComputedOptions<any>
Expand Down
14 changes: 8 additions & 6 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export type CreateComponentPublicInstance<
MakeDefaultsOptional extends boolean = false,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
Attrs extends AttrsType = {},
Attrs extends AttrsType | undefined = undefined,
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
Expand Down Expand Up @@ -215,10 +215,10 @@ export type ComponentPublicInstance<
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
I extends ComponentInjectOptions = {},
S extends SlotsType = {},
Attrs extends AttrsType = {},
PropsAttrs = IsSameType<UnwrapAttrsType<Attrs>, Data> extends true
Attrs extends AttrsType | undefined = undefined,
PropsAttrs = IsSameType<Attrs, undefined> extends true
? {}
: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)>
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)>
> = {
$: ComponentInternalInstance
$data: D
Expand All @@ -227,8 +227,10 @@ export type ComponentPublicInstance<
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults> & PropsAttrs
: P & PublicProps & PropsAttrs
>
$attrs: Omit<UnwrapAttrsType<Attrs>, keyof (P & PublicProps)> &
AllowedComponentProps
$attrs: IsSameType<Attrs, undefined> extends true
? Data
: Omit<UnwrapAttrsType<NonNullable<Attrs>>, keyof (P & PublicProps)> &
AllowedComponentProps
$refs: Data
$slots: UnwrapSlotsType<S>
$root: ComponentPublicInstance | null
Expand Down
13 changes: 5 additions & 8 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ export type VueElementConstructor<P = {}> = {
// so most of the following overloads should be kept in sync w/ defineComponent.

// overload 1: direct setup function
export function defineCustomElement<
Props,
RawBindings = object,
>(
export function defineCustomElement<Props, RawBindings = object>(
setup: (
props: Readonly<Props>,
ctx: SetupContext
) => RawBindings | RenderFunction,
) => RawBindings | RenderFunction
): VueElementConstructor<Props>

// overload 2: object format with no props
Expand All @@ -59,7 +56,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
>(
options: ComponentOptionsWithoutProps<
Props,
Expand Down Expand Up @@ -92,7 +89,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand Down Expand Up @@ -125,7 +122,7 @@ export function defineCustomElement<
I extends ComponentInjectOptions = {},
II extends string = string,
S extends SlotsType = {},
Attrs extends AttrsType = {}
Attrs extends AttrsType | undefined = undefined
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand Down

0 comments on commit df90c7c

Please sign in to comment.