feat(types): define component improvements (#12612)

This commit is contained in:
Evan You 2022-07-05 10:12:21 +08:00 committed by GitHub
parent d45bbeac27
commit fb93c1be77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 1607 additions and 269 deletions

View File

@ -86,7 +86,8 @@ if (typeof Promise !== 'undefined' && isNative(Promise)) {
} }
export function nextTick(): Promise<void> export function nextTick(): Promise<void>
export function nextTick(cb: (...args: any[]) => any, ctx?: object): void export function nextTick<T>(this: T, cb: (this: T, ...args: any[]) => any): void
export function nextTick<T>(cb: (this: T, ...args: any[]) => any, ctx: T): void
/** /**
* @internal * @internal
*/ */

6
types/common.d.ts vendored
View File

@ -13,3 +13,9 @@ type Equal<Left, Right> =
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false; (<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false;
export type HasDefined<T> = Equal<T, unknown> extends true ? false : true export type HasDefined<T> = Equal<T, unknown> extends true ? false : true
// If the the type T accepts type "any", output type Y, otherwise output type N.
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N
export type LooseRequired<T> = { [P in string & keyof T]: T[P] }

11
types/index.d.ts vendored
View File

@ -30,7 +30,8 @@ export {
VNode, VNode,
VNodeComponentOptions, VNodeComponentOptions,
VNodeData, VNodeData,
VNodeDirective VNodeDirective,
ComponentCustomProps
} from './vnode' } from './vnode'
export * from './v3-manual-apis' export * from './v3-manual-apis'
@ -47,13 +48,15 @@ export {
// v2 already has option with same name and it's for a single computed // v2 already has option with same name and it's for a single computed
ComputedOptions as ComponentComputedOptions, ComputedOptions as ComponentComputedOptions,
MethodOptions as ComponentMethodOptions, MethodOptions as ComponentMethodOptions,
ComponentPropsOptions ComponentPropsOptions,
ComponentCustomOptions
} from './v3-component-options' } from './v3-component-options'
export { export {
ComponentInstance, ComponentInstance,
ComponentPublicInstance, ComponentPublicInstance,
ComponentRenderProxy CreateComponentPublicInstance,
} from './v3-component-proxy' ComponentCustomProperties
} from './v3-component-public-instance'
export { export {
// PropType, // PropType,
// PropOptions, // PropOptions,

12
types/jsx.d.ts vendored
View File

@ -1313,7 +1313,12 @@ type NativeElements = {
> >
} }
import { VNode, VNodeData } from './vnode' import {
VNode,
VNodeData,
ComponentCustomProps,
AllowedComponentProps
} from './vnode'
declare global { declare global {
namespace JSX { namespace JSX {
@ -1329,7 +1334,10 @@ declare global {
// @ts-ignore suppress ts:2374 = Duplicate string index signature. // @ts-ignore suppress ts:2374 = Duplicate string index signature.
[name: string]: any [name: string]: any
} }
interface IntrinsicAttributes extends ReservedProps {} interface IntrinsicAttributes
extends ReservedProps,
AllowedComponentProps,
ComponentCustomProps {}
} }
} }

8
types/options.d.ts vendored
View File

@ -2,6 +2,7 @@ import { Vue, CreateElement, CombinedVueInstance } from './vue'
import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode' import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
import { SetupContext } from './v3-setup-context' import { SetupContext } from './v3-setup-context'
import { DebuggerEvent } from './v3-generated' import { DebuggerEvent } from './v3-generated'
import { DefineComponent } from './v3-define-component'
type Constructor = { type Constructor = {
new (...args: any[]): any new (...args: any[]): any
@ -19,6 +20,7 @@ export type Component<
| typeof Vue | typeof Vue
| FunctionalComponentOptions<Props> | FunctionalComponentOptions<Props>
| ComponentOptions<never, Data, Methods, Computed, Props, SetupBindings> | ComponentOptions<never, Data, Methods, Computed, Props, SetupBindings>
| DefineComponent<any, any, any, any, any>
type EsModule<T> = T | { default: T } type EsModule<T> = T | { default: T }
@ -174,7 +176,10 @@ export interface ComponentOptions<
el?: Element | string el?: Element | string
template?: string template?: string
// hack is for functional component type inference, should not be used in user code // hack is for functional component type inference, should not be used in user code
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode render?(
createElement: CreateElement,
hack: RenderContext<Props>
): VNode | null | void
renderError?(createElement: CreateElement, err: Error): VNode renderError?(createElement: CreateElement, err: Error): VNode
staticRenderFns?: ((createElement: CreateElement) => VNode)[] staticRenderFns?: ((createElement: CreateElement) => VNode)[]
@ -198,6 +203,7 @@ export interface ComponentOptions<
[key: string]: [key: string]:
| Component<any, any, any, any> | Component<any, any, any, any>
| AsyncComponent<any, any, any, any> | AsyncComponent<any, any, any, any>
| DefineComponent<any, any, any, any, any, any, any, any, any, any>
} }
transitions?: { [key: string]: object } transitions?: { [key: string]: object }
filters?: { [key: string]: Function } filters?: { [key: string]: Function }

File diff suppressed because it is too large Load Diff

View File

@ -2,11 +2,33 @@ import { Vue } from './vue'
import { VNode } from './vnode' import { VNode } from './vnode'
import { ComponentOptions as Vue2ComponentOptions } from './options' import { ComponentOptions as Vue2ComponentOptions } from './options'
import { EmitsOptions, SetupContext } from './v3-setup-context' import { EmitsOptions, SetupContext } from './v3-setup-context'
import { Data } from './common' import { Data, LooseRequired, UnionToIntersection } from './common'
import { ComponentPropsOptions, ExtractPropTypes } from './v3-component-props' import {
import { ComponentRenderProxy } from './v3-component-proxy' ComponentPropsOptions,
ExtractDefaultPropTypes,
ExtractPropTypes
} from './v3-component-props'
import { CreateComponentPublicInstance } from './v3-component-public-instance'
export { ComponentPropsOptions } from './v3-component-props' export { ComponentPropsOptions } from './v3-component-props'
/**
* Interface for declaring custom options.
*
* @example
* ```ts
* declare module 'vue' {
* interface ComponentCustomOptions {
* beforeRouteUpdate?(
* to: Route,
* from: Route,
* next: () => void
* ): void
* }
* }
* ```
*/
export interface ComponentCustomOptions {}
export type ComputedGetter<T> = (ctx?: any) => T export type ComputedGetter<T> = (ctx?: any) => T
export type ComputedSetter<T> = (v: T) => void export type ComputedSetter<T> = (v: T) => void
@ -34,24 +56,76 @@ export type SetupFunction<
ctx: SetupContext<Emits> ctx: SetupContext<Emits>
) => RawBindings | (() => VNode | null) | void ) => RawBindings | (() => VNode | null) | void
interface ComponentOptionsBase< type ExtractOptionProp<T> = T extends ComponentOptionsBase<
Props, infer P, // Props
D = Data, any, // RawBindings
C extends ComputedOptions = {}, any, // D
M extends MethodOptions = {} any, // C
> extends Omit< any, // M
Vue2ComponentOptions<Vue, D, M, C, Props>, any, // Mixin
'data' | 'computed' | 'method' | 'setup' | 'props' any, // Extends
> { any, // EmitsOptions
// allow any custom options any // Defaults
[key: string]: any >
? unknown extends P
? {}
: P
: {}
export interface ComponentOptionsBase<
Props,
RawBindings,
D,
C extends ComputedOptions,
M extends MethodOptions,
Mixin extends ComponentOptionsMixin,
Extends extends ComponentOptionsMixin,
Emits extends EmitsOptions,
EmitNames extends string = string,
Defaults = {}
> extends Omit<
Vue2ComponentOptions<Vue, D, M, C, Props>,
'data' | 'computed' | 'methods' | 'setup' | 'props' | 'mixins' | 'extends'
>,
ComponentCustomOptions {
// rewrite options api types // rewrite options api types
data?: (this: Props & Vue, vm: Props) => D data?: (
this: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>,
vm: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>
) => D
computed?: C computed?: C
methods?: M methods?: M
mixins?: Mixin[]
extends?: Extends
emits?: (Emits | EmitNames[]) & ThisType<void>
setup?: SetupFunction<
Readonly<
LooseRequired<
Props &
UnionToIntersection<ExtractOptionProp<Mixin>> &
UnionToIntersection<ExtractOptionProp<Extends>>
>
>,
RawBindings,
Emits
>
__defaults?: Defaults
} }
export type ComponentOptionsMixin = ComponentOptionsBase<
any,
any,
any,
any,
any,
any,
any,
any,
any,
any
>
export type ExtractComputedReturns<T extends any> = { export type ExtractComputedReturns<T extends any> = {
[key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn } [key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
? TReturn ? TReturn
@ -66,17 +140,36 @@ export type ComponentOptionsWithProps<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string, EmitsNames extends string = string,
Props = ExtractPropTypes<PropsOptions> Props = ExtractPropTypes<PropsOptions>,
> = ComponentOptionsBase<Props, D, C, M> & { Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits,
EmitsNames,
Defaults
> & {
props?: PropsOptions props?: PropsOptions
emits?: (Emits | EmitsNames[]) & ThisType<void>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType< } & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits> CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits
>
> >
export type ComponentOptionsWithArrayProps< export type ComponentOptionsWithArrayProps<
@ -85,17 +178,35 @@ export type ComponentOptionsWithArrayProps<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string, EmitsNames extends string = string,
Props = Readonly<{ [key in PropNames]?: any }> Props = Readonly<{ [key in PropNames]?: any }>
> = ComponentOptionsBase<Props, D, C, M> & { > = ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits,
EmitsNames,
{}
> & {
props?: PropNames[] props?: PropNames[]
emits?: (Emits | EmitsNames[]) & ThisType<void>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType< } & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits> CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits
>
> >
export type ComponentOptionsWithoutProps< export type ComponentOptionsWithoutProps<
@ -104,16 +215,34 @@ export type ComponentOptionsWithoutProps<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string EmitsNames extends string = string
> = ComponentOptionsBase<Props, D, C, M> & { > = ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits,
EmitsNames,
{}
> & {
props?: undefined props?: undefined
emits?: (Emits | EmitsNames[]) & ThisType<void>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType< } & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits> CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
Emits
>
> >
export type WithLegacyAPI<T, D, C, M, Props> = T & export type WithLegacyAPI<T, D, C, M, Props> = T &

View File

@ -1,4 +1,4 @@
import { Data } from './common' import { Data, IfAny } from './common'
export type ComponentPropsOptions<P = Data> = export type ComponentPropsOptions<P = Data> =
| ComponentObjectPropsOptions<P> | ComponentObjectPropsOptions<P>
@ -48,26 +48,25 @@ type ExtractCorrectPropType<T> = T extends Function
? ExtractFunctionPropType<T> ? ExtractFunctionPropType<T>
: Exclude<T, Function> : Exclude<T, Function>
// prettier-ignore type InferPropType<T> = [T] extends [null]
type InferPropType<T> = T extends null
? any // null & true would fail to infer ? any // null & true would fail to infer
: T extends { type: null | true } : [T] extends [{ type: null | true }]
? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean` ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`
: T extends ObjectConstructor | { type: ObjectConstructor } : [T] extends [ObjectConstructor | { type: ObjectConstructor }]
? Record<string, any> ? Record<string, any>
: T extends BooleanConstructor | { type: BooleanConstructor } : [T] extends [BooleanConstructor | { type: BooleanConstructor }]
? boolean ? boolean
: T extends DateConstructor | { type: DateConstructor} : [T] extends [DateConstructor | { type: DateConstructor }]
? Date ? Date
: T extends FunctionConstructor : [T] extends [(infer U)[] | { type: (infer U)[] }]
? Function ? U extends DateConstructor
: T extends Prop<infer V, infer D> ? Date | InferPropType<U>
? unknown extends V : InferPropType<U>
? D extends null | undefined : [T] extends [Prop<infer V, infer D>]
? V ? unknown extends V
: D ? IfAny<V, V, D>
: ExtractCorrectPropType<V> : V
: T : T
export type ExtractPropTypes<O> = { export type ExtractPropTypes<O> = {
// use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to support IDE features // use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to support IDE features

View File

@ -1,189 +0,0 @@
import { ExtractDefaultPropTypes, ExtractPropTypes } from './v3-component-props'
import {
nextTick,
ShallowUnwrapRef,
UnwrapNestedRefs,
WatchOptions,
WatchStopHandle
} from './v3-generated'
import { Data } from './common'
import { Vue, VueConstructor } from './vue'
import { ComponentOptions as Vue2ComponentOptions } from './options'
import {
ComputedOptions,
MethodOptions,
ExtractComputedReturns
} from './v3-component-options'
import {
ComponentRenderEmitFn,
EmitFn,
EmitsOptions,
ObjectEmitsOptions,
Slots
} from './v3-setup-context'
type EmitsToProps<T extends EmitsOptions> = T extends string[]
? {
[K in string & `on${Capitalize<T[number]>}`]?: (...args: any[]) => any
}
: T extends ObjectEmitsOptions
? {
[K in string &
`on${Capitalize<string & keyof T>}`]?: K extends `on${infer C}`
? T[Uncapitalize<C>] extends null
? (...args: any[]) => any
: (
...args: T[Uncapitalize<C>] extends (...args: infer P) => any
? P
: never
) => any
: never
}
: {}
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 = {}, // props type extracted from props option
B = {}, // raw bindings returned from setup()
D = {}, // return from data()
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin = {},
Extends = {},
Emits extends EmitsOptions = {},
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false
> = {
$data: D
$props: Readonly<
MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
>
$attrs: Record<string, string>
$emit: ComponentRenderEmitFn<
Emits,
keyof Emits,
ComponentRenderProxy<
P,
B,
D,
C,
M,
Mixin,
Extends,
Emits,
PublicProps,
Defaults,
MakeDefaultsOptional
>
>
} & Readonly<P> &
ShallowUnwrapRef<B> &
D &
M &
ExtractComputedReturns<C> &
Omit<Vue, '$data' | '$props' | '$attrs' | '$emit'>
// for Vetur and TSX support
type VueConstructorProxy<
PropsOptions,
RawBindings,
Data,
Computed extends ComputedOptions,
Methods extends MethodOptions,
Mixin = {},
Extends = {},
Emits extends EmitsOptions = {},
Props = ExtractPropTypes<PropsOptions> &
({} extends Emits ? {} : EmitsToProps<Emits>)
> = Omit<VueConstructor, never> & {
new (...args: any[]): ComponentRenderProxy<
Props,
ShallowUnwrapRef<RawBindings>,
Data,
Computed,
Methods,
Mixin,
Extends,
Emits,
Props,
ExtractDefaultPropTypes<PropsOptions>,
true
>
}
type DefaultData<V> = object | ((this: V) => object)
type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any }
type DefaultComputed = { [key: string]: any }
export type VueProxy<
PropsOptions,
RawBindings,
Data = DefaultData<Vue>,
Computed extends ComputedOptions = DefaultComputed,
Methods extends MethodOptions = DefaultMethods<Vue>,
Mixin = {},
Extends = {},
Emits extends EmitsOptions = {}
> = Vue2ComponentOptions<
Vue,
ShallowUnwrapRef<RawBindings> & Data,
Methods,
Computed,
PropsOptions,
ExtractPropTypes<PropsOptions>
> &
VueConstructorProxy<
PropsOptions,
RawBindings,
Data,
Computed,
Methods,
Mixin,
Extends,
Emits
>
// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
P = {}, // props type extracted from props option
B = {}, // raw bindings returned from setup()
D = {}, // return from data()
C extends ComputedOptions = {},
M extends MethodOptions = {},
E extends EmitsOptions = {},
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false
> = {
$data: D
$props: MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
$attrs: Data
$refs: Data
$slots: Slots
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
$emit: EmitFn<E>
$el: any
// $options: Options & MergedComponentOptionsOverride
$forceUpdate: () => void
$nextTick: typeof nextTick
$watch(
source: string | Function,
cb: Function,
options?: WatchOptions
): WatchStopHandle
} & P &
ShallowUnwrapRef<B> &
UnwrapNestedRefs<D> &
ExtractComputedReturns<C> &
M

230
types/v3-component-public-instance.d.ts vendored Normal file
View File

@ -0,0 +1,230 @@
import { ExtractDefaultPropTypes, ExtractPropTypes } from './v3-component-props'
import {
DebuggerEvent,
nextTick,
ShallowUnwrapRef,
UnwrapNestedRefs,
WatchOptions,
WatchStopHandle
} from './v3-generated'
import { Data, UnionToIntersection } from './common'
import { VueConstructor } from './vue'
import {
ComputedOptions,
MethodOptions,
ExtractComputedReturns,
ComponentOptionsMixin,
ComponentOptionsBase
} from './v3-component-options'
import { EmitFn, EmitsOptions, Slots } from './v3-setup-context'
/**
* Custom properties added to component instances in any way and can be accessed through `this`
*
* @example
* ```ts
* import { Router } from 'vue-router'
*
* declare module 'vue' {
* interface ComponentCustomProperties {
* $router: Router
* }
* }
* ```
*/
export interface ComponentCustomProperties {}
export type ComponentInstance = InstanceType<VueConstructor>
export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M' | 'Defaults'
export type OptionTypesType<
P = {},
B = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Defaults = {}
> = {
P: P
B: B
D: D
C: C
M: M
Defaults: Defaults
}
type IsDefaultMixinComponent<T> = T extends ComponentOptionsMixin
? ComponentOptionsMixin extends T
? true
: false
: false
type MixinToOptionTypes<T> = T extends ComponentOptionsBase<
infer P,
infer B,
infer D,
infer C,
infer M,
infer Mixin,
infer Extends,
any,
any,
infer Defaults
>
? OptionTypesType<P & {}, B & {}, D & {}, C & {}, M & {}, Defaults & {}> &
IntersectionMixin<Mixin> &
IntersectionMixin<Extends>
: never
// ExtractMixin(map type) is used to resolve circularly references
type ExtractMixin<T> = {
Mixin: MixinToOptionTypes<T>
}[T extends ComponentOptionsMixin ? 'Mixin' : never]
type IntersectionMixin<T> = IsDefaultMixinComponent<T> extends true
? OptionTypesType<{}, {}, {}, {}, {}, {}>
: UnionToIntersection<ExtractMixin<T>>
type UnwrapMixinsType<
T,
Type extends OptionTypesKeys
> = T extends OptionTypesType ? T[Type] : never
type EnsureNonVoid<T> = T extends void ? {} : T
export type CreateComponentPublicInstance<
P = {},
B = {},
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false,
PublicMixin = IntersectionMixin<Mixin> & IntersectionMixin<Extends>,
PublicP = UnwrapMixinsType<PublicMixin, 'P'> & EnsureNonVoid<P>,
PublicB = UnwrapMixinsType<PublicMixin, 'B'> & EnsureNonVoid<B>,
PublicD = UnwrapMixinsType<PublicMixin, 'D'> & EnsureNonVoid<D>,
PublicC extends ComputedOptions = UnwrapMixinsType<PublicMixin, 'C'> &
EnsureNonVoid<C>,
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
EnsureNonVoid<M>,
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
EnsureNonVoid<Defaults>
> = ComponentPublicInstance<
PublicP,
PublicB,
PublicD,
PublicC,
PublicM,
E,
PublicProps,
PublicDefaults,
MakeDefaultsOptional
>
// public properties exposed on the proxy, which is used as the render context
// in templates (as `this` in the render option)
export type ComponentPublicInstance<
P = {}, // props type extracted from props option
B = {}, // raw bindings returned from setup()
D = {}, // return from data()
C extends ComputedOptions = {},
M extends MethodOptions = {},
E extends EmitsOptions = {},
PublicProps = P,
Defaults = {},
MakeDefaultsOptional extends boolean = false,
Options = ComponentOptionsBase<
any,
any,
any,
any,
any,
any,
any,
any,
any,
any
>
> = {
// $: ComponentInternalInstance
$data: D
$props: Readonly<
MakeDefaultsOptional extends true
? Partial<Defaults> & Omit<P & PublicProps, keyof Defaults>
: P & PublicProps
>
$attrs: Data
$refs: Data
$slots: Slots
$root: ComponentPublicInstance | null
$parent: ComponentPublicInstance | null
$emit: EmitFn<E>
$el: any
$options: Options & MergedComponentOptionsOverride
$forceUpdate: () => void
$nextTick: typeof nextTick
$watch(
source: string | Function,
cb: Function,
options?: WatchOptions
): WatchStopHandle
} & Readonly<P> &
ShallowUnwrapRef<B> &
UnwrapNestedRefs<D> &
ExtractComputedReturns<C> &
M &
ComponentCustomProperties
type MergedHook<T = () => void> = T | T[]
export type MergedComponentOptionsOverride = {
beforeCreate?: MergedHook
created?: MergedHook
beforeMount?: MergedHook
mounted?: MergedHook
beforeUpdate?: MergedHook
updated?: MergedHook
activated?: MergedHook
deactivated?: MergedHook
/** @deprecated use `beforeUnmount` instead */
beforeDestroy?: MergedHook
beforeUnmount?: MergedHook
/** @deprecated use `unmounted` instead */
destroyed?: MergedHook
unmounted?: MergedHook
renderTracked?: MergedHook<DebuggerHook>
renderTriggered?: MergedHook<DebuggerHook>
errorCaptured?: MergedHook<ErrorCapturedHook>
}
export type DebuggerHook = (e: DebuggerEvent) => void
export type ErrorCapturedHook<TError = unknown> = (
err: TError,
instance: ComponentPublicInstance | null,
info: string
) => boolean | void
export type ComponentPublicInstanceConstructor<
T extends ComponentPublicInstance<
Props,
RawBindings,
D,
C,
M
> = ComponentPublicInstance<any, any, any>,
Props = any,
RawBindings = any,
D = any,
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions
> = {
new (...args: any[]): T
}

View File

@ -1,15 +1,72 @@
import { ComponentPropsOptions } from './v3-component-props' import { Component } from '..'
import {
ComponentPropsOptions,
ExtractDefaultPropTypes,
ExtractPropTypes
} from './v3-component-props'
import { import {
MethodOptions, MethodOptions,
ComputedOptions, ComputedOptions,
ComponentOptionsWithoutProps, ComponentOptionsWithoutProps,
ComponentOptionsWithArrayProps, ComponentOptionsWithArrayProps,
ComponentOptionsWithProps ComponentOptionsWithProps,
ComponentOptionsMixin,
ComponentOptionsBase
} from './v3-component-options' } from './v3-component-options'
import { VueProxy } from './v3-component-proxy' import {
ComponentPublicInstanceConstructor,
CreateComponentPublicInstance
} from './v3-component-public-instance'
import { Data, HasDefined } from './common' import { Data, HasDefined } from './common'
import { EmitsOptions } from './v3-setup-context' import { EmitsOptions } from './v3-setup-context'
type DefineComponent<
PropsOrPropOptions = {},
RawBindings = {},
D = {},
C extends ComputedOptions = ComputedOptions,
M extends MethodOptions = MethodOptions,
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {},
EE extends string = string,
Props = Readonly<
PropsOrPropOptions extends ComponentPropsOptions
? ExtractPropTypes<PropsOrPropOptions>
: PropsOrPropOptions
>,
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
> = ComponentPublicInstanceConstructor<
CreateComponentPublicInstance<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
Props,
Defaults,
true
> &
Props
> &
ComponentOptionsBase<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Defaults
> & {
props: PropsOrPropOptions
}
/** /**
* overload 1: object format with no props * overload 1: object format with no props
*/ */
@ -18,8 +75,8 @@ export function defineComponent<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string EmitsNames extends string = string
>( >(
@ -34,7 +91,8 @@ export function defineComponent<
Emits, Emits,
EmitsNames EmitsNames
> >
): VueProxy<{}, RawBindings, D, C, M, Mixin, Extends, Emits> ): DefineComponent<{}, RawBindings, D, C, M, Mixin, Extends, Emits>
/** /**
* overload 2: object format with array props declaration * overload 2: object format with array props declaration
* props inferred as `{ [key in PropNames]?: any }` * props inferred as `{ [key in PropNames]?: any }`
@ -47,8 +105,8 @@ export function defineComponent<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string, EmitsNames extends string = string,
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
@ -64,7 +122,7 @@ export function defineComponent<
Emits, Emits,
EmitsNames EmitsNames
> >
): VueProxy< ): DefineComponent<
Readonly<{ [key in PropNames]?: any }>, Readonly<{ [key in PropNames]?: any }>,
RawBindings, RawBindings,
D, D,
@ -86,8 +144,8 @@ export function defineComponent<
D = Data, D = Data,
C extends ComputedOptions = {}, C extends ComputedOptions = {},
M extends MethodOptions = {}, M extends MethodOptions = {},
Mixin = {}, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends = {}, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
Emits extends EmitsOptions = {}, Emits extends EmitsOptions = {},
EmitsNames extends string = string, EmitsNames extends string = string,
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
@ -116,4 +174,4 @@ export function defineComponent<
Emits, Emits,
EmitsNames EmitsNames
> >
): VueProxy<PropsOptions, RawBindings, D, C, M, Mixin, Extends, Emits> ): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, Emits>

View File

@ -29,12 +29,6 @@ export type EmitFn<
}[Event] }[Event]
> >
export type ComponentRenderEmitFn<
Options = ObjectEmitsOptions,
Event extends keyof Options = keyof Options,
T extends Vue | void = void
> = EmitFn<Options, Event, T>
export interface SetupContext<E extends EmitsOptions = {}> { export interface SetupContext<E extends EmitsOptions = {}> {
attrs: Data attrs: Data
slots: Slots slots: Slots

13
types/vnode.d.ts vendored
View File

@ -1,6 +1,19 @@
import { Vue } from './vue' import { Vue } from './vue'
import { DirectiveFunction, DirectiveOptions } from './options' import { DirectiveFunction, DirectiveOptions } from './options'
/**
* For extending allowed non-declared props on components in TSX
*/
export interface ComponentCustomProps {}
/**
* Default allowed non-declared props on component in TSX
*/
export interface AllowedComponentProps {
class?: unknown
style?: unknown
}
export type ScopedSlot = (props: any) => ScopedSlotReturnValue export type ScopedSlot = (props: any) => ScopedSlotReturnValue
type ScopedSlotReturnValue = type ScopedSlotReturnValue =
| VNode | VNode