Skip to content

Commit

Permalink
fix(types): preserve and expose original options on defineComponent r…
Browse files Browse the repository at this point in the history
…eturn type (#5416)

fix #3796
  • Loading branch information
Zclhlmgqzc authored May 6, 2022
1 parent 32e53bf commit 98b821d
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 156 deletions.
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiAsyncComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function defineAsyncComponent<
)
}

return defineComponent({
return defineComponent<{}>({
name: 'AsyncComponentWrapper',

__asyncLoader: load,
Expand Down
155 changes: 99 additions & 56 deletions packages/runtime-core/src/apiDefineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ComponentOptionsWithObjectProps,
ComponentOptionsMixin,
RenderFunction,
ComponentOptionsBase
ComponentOptionsBase,
ComponentProvideOptions
} from './componentOptions'
import {
SetupContext,
Expand Down Expand Up @@ -40,6 +41,8 @@ export type DefineComponent<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
RawOptions extends {} = {},
PP = PublicProps,
Props = Readonly<
PropsOrPropOptions extends ComponentPropsOptions
Expand All @@ -48,22 +51,23 @@ export type DefineComponent<
> &
({} extends E ? {} : EmitsToProps<E>),
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP & Props,
Defaults,
true
> = RawOptions &
ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
PP & Props,
Defaults,
true
> &
Props
> &
Props
> &
ComponentOptionsBase<
Props,
RawBindings,
Expand All @@ -74,7 +78,8 @@ export type DefineComponent<
Extends,
E,
EE,
Defaults
Defaults,
Provide
> &
PP

Expand Down Expand Up @@ -104,20 +109,36 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
>(
options: ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
options: Options &
ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
): DefineComponent<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide,
Options
>

// overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: any }
Expand All @@ -131,19 +152,23 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
>(
options: ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
options: Options &
ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
): DefineComponent<
Readonly<{ [key in PropNames]?: any }>,
RawBindings,
Expand All @@ -153,7 +178,9 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
Provide,
Options
>

// overload 4: object format with object props declaration
Expand All @@ -169,20 +196,36 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
options: Options &
ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
): DefineComponent<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide,
Options
>

// implementation, close to no-op
export function defineComponent(options: unknown) {
Expand Down
26 changes: 19 additions & 7 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ export interface ComponentOptionsBase<
Extends extends ComponentOptionsMixin,
E extends EmitsOptions,
EE extends string = string,
Defaults = {}
> extends LegacyOptions<Props, D, C, M, Mixin, Extends>,
Defaults = {},
Provide extends ComponentProvideOptions = ComponentProvideOptions
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, Provide>,
ComponentInternalOptions,
ComponentCustomOptions {
setup?: (
Expand Down Expand Up @@ -224,6 +225,7 @@ export type ComponentOptionsWithoutProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
PE = Props & EmitsToProps<E>
> = ComponentOptionsBase<
PE,
Expand All @@ -235,7 +237,8 @@ export type ComponentOptionsWithoutProps<
Extends,
E,
EE,
{}
{},
Provide
> & {
props?: undefined
} & ThisType<
Expand All @@ -252,6 +255,7 @@ export type ComponentOptionsWithArrayProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
> = ComponentOptionsBase<
Props,
Expand All @@ -263,7 +267,8 @@ export type ComponentOptionsWithArrayProps<
Extends,
E,
EE,
{}
{},
Provide
> & {
props: PropNames[]
} & ThisType<
Expand All @@ -289,6 +294,7 @@ export type ComponentOptionsWithObjectProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Expand All @@ -301,7 +307,8 @@ export type ComponentOptionsWithObjectProps<
Extends,
E,
EE,
Defaults
Defaults,
Provide
> & {
props: PropsOptions & ThisType<void>
} & ThisType<
Expand Down Expand Up @@ -384,6 +391,10 @@ type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]

type ComponentWatchOptions = Record<string, ComponentWatchOptionItem>

export type ComponentProvideOptions = ObjectProvideOptions | Function

type ObjectProvideOptions = Record<string | symbol, unknown>

type ComponentInjectOptions = string[] | ObjectInjectOptions

type ObjectInjectOptions = Record<
Expand All @@ -397,7 +408,8 @@ interface LegacyOptions<
C extends ComputedOptions,
M extends MethodOptions,
Mixin extends ComponentOptionsMixin,
Extends extends ComponentOptionsMixin
Extends extends ComponentOptionsMixin,
Provide extends ComponentProvideOptions = ComponentProvideOptions
> {
compatConfig?: CompatConfig

Expand Down Expand Up @@ -431,7 +443,7 @@ interface LegacyOptions<
computed?: C
methods?: M
watch?: ComponentWatchOptions
provide?: Data | Function
provide?: Provide
inject?: ComponentInjectOptions

// assets
Expand Down
20 changes: 17 additions & 3 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
OptionTypesKeys,
resolveMergedOptions,
shouldCacheAccess,
MergedComponentOptionsOverride
MergedComponentOptionsOverride,
ComponentProvideOptions
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
import { Slots } from './componentSlots'
Expand Down Expand Up @@ -150,7 +151,8 @@ export type CreateComponentPublicInstance<
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
EnsureNonVoid<M>,
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
EnsureNonVoid<Defaults>
EnsureNonVoid<Defaults>,
Provide extends ComponentProvideOptions = ComponentProvideOptions
> = ComponentPublicInstance<
PublicP,
PublicB,
Expand All @@ -161,7 +163,19 @@ export type CreateComponentPublicInstance<
PublicProps,
PublicDefaults,
MakeDefaultsOptional,
ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>
ComponentOptionsBase<
P,
B,
D,
C,
M,
Mixin,
Extends,
E,
string,
Defaults,
Provide
>
>

// public properties exposed on the proxy, which is used as the render context
Expand Down
Loading

0 comments on commit 98b821d

Please sign in to comment.