Skip to content

Commit

Permalink
fix(types): support inferring injected properties in options api (vue…
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyxu1102 authored and chrislone committed Feb 4, 2023
1 parent 51ea28e commit 7b15cbd
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 33 deletions.
27 changes: 20 additions & 7 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,
ComponentInjectOptions
} from './componentOptions'
import {
SetupContext,
Expand Down Expand Up @@ -104,7 +105,9 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string
>(
options: ComponentOptionsWithoutProps<
Props,
Expand All @@ -115,7 +118,9 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
I,
II
>
): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>

Expand All @@ -131,7 +136,9 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand All @@ -142,7 +149,9 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
I,
II
>
): DefineComponent<
Readonly<{ [key in PropNames]?: any }>,
Expand All @@ -169,7 +178,9 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand All @@ -180,7 +191,9 @@ export function defineComponent<
Mixin,
Extends,
E,
EE
EE,
I,
II
>
): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>

Expand Down
57 changes: 44 additions & 13 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ export interface ComponentOptionsBase<
Extends extends ComponentOptionsMixin,
E extends EmitsOptions,
EE extends string = string,
Defaults = {}
> extends LegacyOptions<Props, D, C, M, Mixin, Extends>,
Defaults = {},
I extends ComponentInjectOptions = {},
II extends string = string
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, I, II>,
ComponentInternalOptions,
ComponentCustomOptions {
setup?: (
Expand Down Expand Up @@ -225,7 +227,9 @@ export type ComponentOptionsWithoutProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
PE = Props & EmitsToProps<E>
I extends ComponentInjectOptions = {},
II extends string = string,
PE = Props & EmitsToProps<E>,
> = ComponentOptionsBase<
PE,
RawBindings,
Expand All @@ -236,11 +240,13 @@ export type ComponentOptionsWithoutProps<
Extends,
E,
EE,
{}
{},
I,
II
> & {
props?: undefined
} & ThisType<
CreateComponentPublicInstance<PE, RawBindings, D, C, M, Mixin, Extends, E>
CreateComponentPublicInstance<PE, RawBindings, D, C, M, Mixin, Extends, E, PE, {}, false, I>
>

export type ComponentOptionsWithArrayProps<
Expand All @@ -253,6 +259,8 @@ export type ComponentOptionsWithArrayProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
> = ComponentOptionsBase<
Props,
Expand All @@ -264,7 +272,9 @@ export type ComponentOptionsWithArrayProps<
Extends,
E,
EE,
{}
{},
I,
II
> & {
props: PropNames[]
} & ThisType<
Expand All @@ -276,7 +286,11 @@ export type ComponentOptionsWithArrayProps<
M,
Mixin,
Extends,
E
E,
Props,
{},
false,
I
>
>

Expand All @@ -290,8 +304,10 @@ export type ComponentOptionsWithObjectProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string,
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
Defaults = ExtractDefaultPropTypes<PropsOptions>
Defaults = ExtractDefaultPropTypes<PropsOptions>,
> = ComponentOptionsBase<
Props,
RawBindings,
Expand All @@ -302,7 +318,9 @@ export type ComponentOptionsWithObjectProps<
Extends,
E,
EE,
Defaults
Defaults,
I,
II
> & {
props: PropsOptions & ThisType<void>
} & ThisType<
Expand All @@ -317,7 +335,8 @@ export type ComponentOptionsWithObjectProps<
E,
Props,
Defaults,
false
false,
I
>
>

Expand Down Expand Up @@ -389,20 +408,32 @@ export type ComponentProvideOptions = ObjectProvideOptions | Function

type ObjectProvideOptions = Record<string | symbol, unknown>

type ComponentInjectOptions = string[] | ObjectInjectOptions
export type ComponentInjectOptions = string[] | ObjectInjectOptions

type ObjectInjectOptions = Record<
string | symbol,
string | symbol | { from?: string | symbol; default?: unknown }
>

export type InjectToObject<T extends ComponentInjectOptions> = T extends string[]
? {
[K in T[number]]?: unknown
}
: T extends ObjectInjectOptions
? {
[K in keyof T]?: unknown
}
: never

interface LegacyOptions<
Props,
D,
C extends ComputedOptions,
M extends MethodOptions,
Mixin extends ComponentOptionsMixin,
Extends extends ComponentOptionsMixin
Extends extends ComponentOptionsMixin,
I extends ComponentInjectOptions,
II extends string
> {
compatConfig?: CompatConfig

Expand Down Expand Up @@ -437,7 +468,7 @@ interface LegacyOptions<
methods?: M
watch?: ComponentWatchOptions
provide?: ComponentProvideOptions
inject?: ComponentInjectOptions
inject?: I | II[]

// assets
filters?: Record<string, Function>
Expand Down
16 changes: 11 additions & 5 deletions packages/runtime-core/src/componentPublicInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
OptionTypesKeys,
resolveMergedOptions,
shouldCacheAccess,
MergedComponentOptionsOverride
MergedComponentOptionsOverride,
InjectToObject,
ComponentInjectOptions
} from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits'
import { Slots } from './componentSlots'
Expand Down Expand Up @@ -141,6 +143,7 @@ export type CreateComponentPublicInstance<
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false,
I extends ComponentInjectOptions = {},
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
Expand All @@ -150,7 +153,7 @@ export type CreateComponentPublicInstance<
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
EnsureNonVoid<M>,
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
EnsureNonVoid<Defaults>
EnsureNonVoid<Defaults>,
> = ComponentPublicInstance<
PublicP,
PublicB,
Expand All @@ -161,7 +164,8 @@ 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>,
I
>

// public properties exposed on the proxy, which is used as the render context
Expand All @@ -176,7 +180,8 @@ export type ComponentPublicInstance<
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false,
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>
Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
I extends ComponentInjectOptions = {}
> = {
$: ComponentInternalInstance
$data: D
Expand Down Expand Up @@ -205,7 +210,8 @@ export type ComponentPublicInstance<
UnwrapNestedRefs<D> &
ExtractComputedReturns<C> &
M &
ComponentCustomProperties
ComponentCustomProperties &
InjectToObject<I>

export type PublicPropertiesMap = Record<
string,
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ export {
RenderFunction,
MethodOptions,
ComputedOptions,
RuntimeCompilerOptions
RuntimeCompilerOptions,
ComponentInjectOptions
} from './componentOptions'
export { EmitsOptions, ObjectEmitsOptions } from './componentEmits'
export {
Expand Down
27 changes: 20 additions & 7 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
nextTick,
warn,
ConcreteComponent,
ComponentOptions
ComponentOptions,
ComponentInjectOptions
} from '@vue/runtime-core'
import { camelize, extend, hyphenate, isArray, toNumber } from '@vue/shared'
import { hydrate, render } from '.'
Expand Down Expand Up @@ -49,7 +50,9 @@ export function defineCustomElement<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions,
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string
>(
options: ComponentOptionsWithoutProps<
Props,
Expand All @@ -60,7 +63,9 @@ export function defineCustomElement<
Mixin,
Extends,
E,
EE
EE,
I,
II
> & { styles?: string[] }
): VueElementConstructor<Props>

Expand All @@ -74,7 +79,9 @@ export function defineCustomElement<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string
>(
options: ComponentOptionsWithArrayProps<
PropNames,
Expand All @@ -85,7 +92,9 @@ export function defineCustomElement<
Mixin,
Extends,
E,
EE
EE,
I,
II
> & { styles?: string[] }
): VueElementConstructor<{ [K in PropNames]: any }>

Expand All @@ -99,7 +108,9 @@ export function defineCustomElement<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>,
EE extends string = string
EE extends string = string,
I extends ComponentInjectOptions = {},
II extends string = string
>(
options: ComponentOptionsWithObjectProps<
PropsOptions,
Expand All @@ -110,7 +121,9 @@ export function defineCustomElement<
Mixin,
Extends,
E,
EE
EE,
I,
II
> & { styles?: string[] }
): VueElementConstructor<ExtractPropTypes<PropsOptions>>

Expand Down
Loading

0 comments on commit 7b15cbd

Please sign in to comment.