feat(types): mixins/extends support in TypeScript (#626)
This commit is contained in:
@@ -48,7 +48,10 @@ import {
|
||||
} from './componentProps'
|
||||
import { EmitsOptions } from './componentEmits'
|
||||
import { Directive } from './directives'
|
||||
import { ComponentPublicInstance } from './componentProxy'
|
||||
import {
|
||||
CreateComponentPublicInstance,
|
||||
ComponentPublicInstance
|
||||
} from './componentProxy'
|
||||
import { warn } from './warning'
|
||||
import { VNodeChild } from './vnode'
|
||||
|
||||
@@ -78,10 +81,12 @@ export interface ComponentOptionsBase<
|
||||
D,
|
||||
C extends ComputedOptions,
|
||||
M extends MethodOptions,
|
||||
Mixin extends ComponentOptionsMixin,
|
||||
Extends extends ComponentOptionsMixin,
|
||||
E extends EmitsOptions,
|
||||
EE extends string = string
|
||||
>
|
||||
extends LegacyOptions<Props, D, C, M>,
|
||||
extends LegacyOptions<Props, D, C, M, Mixin, Extends>,
|
||||
SFCInternalOptions,
|
||||
ComponentCustomOptions {
|
||||
setup?: (
|
||||
@@ -148,12 +153,24 @@ export type ComponentOptionsWithoutProps<
|
||||
D = {},
|
||||
C extends ComputedOptions = {},
|
||||
M extends MethodOptions = {},
|
||||
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
E extends EmitsOptions = EmitsOptions,
|
||||
EE extends string = string
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, E, EE> & {
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> & {
|
||||
props?: undefined
|
||||
} & ThisType<
|
||||
ComponentPublicInstance<{}, RawBindings, D, C, M, E, Readonly<Props>>
|
||||
CreateComponentPublicInstance<
|
||||
{},
|
||||
RawBindings,
|
||||
D,
|
||||
C,
|
||||
M,
|
||||
Mixin,
|
||||
Extends,
|
||||
E,
|
||||
Readonly<Props>
|
||||
>
|
||||
>
|
||||
|
||||
export type ComponentOptionsWithArrayProps<
|
||||
@@ -162,12 +179,25 @@ export type ComponentOptionsWithArrayProps<
|
||||
D = {},
|
||||
C extends ComputedOptions = {},
|
||||
M extends MethodOptions = {},
|
||||
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
E extends EmitsOptions = EmitsOptions,
|
||||
EE extends string = string,
|
||||
Props = Readonly<{ [key in PropNames]?: any }>
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, E, EE> & {
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> & {
|
||||
props: PropNames[]
|
||||
} & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M, E>>
|
||||
} & ThisType<
|
||||
CreateComponentPublicInstance<
|
||||
Props,
|
||||
RawBindings,
|
||||
D,
|
||||
C,
|
||||
M,
|
||||
Mixin,
|
||||
Extends,
|
||||
E
|
||||
>
|
||||
>
|
||||
|
||||
export type ComponentOptionsWithObjectProps<
|
||||
PropsOptions = ComponentObjectPropsOptions,
|
||||
@@ -175,18 +205,43 @@ export type ComponentOptionsWithObjectProps<
|
||||
D = {},
|
||||
C extends ComputedOptions = {},
|
||||
M extends MethodOptions = {},
|
||||
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
|
||||
E extends EmitsOptions = EmitsOptions,
|
||||
EE extends string = string,
|
||||
Props = Readonly<ExtractPropTypes<PropsOptions>>
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, E, EE> & {
|
||||
> = ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> & {
|
||||
props: PropsOptions
|
||||
} & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M, E>>
|
||||
} & ThisType<
|
||||
CreateComponentPublicInstance<
|
||||
Props,
|
||||
RawBindings,
|
||||
D,
|
||||
C,
|
||||
M,
|
||||
Mixin,
|
||||
Extends,
|
||||
E
|
||||
>
|
||||
>
|
||||
|
||||
export type ComponentOptions =
|
||||
| ComponentOptionsWithoutProps<any, any, any, any, any>
|
||||
| ComponentOptionsWithObjectProps<any, any, any, any, any>
|
||||
| ComponentOptionsWithArrayProps<any, any, any, any, any>
|
||||
|
||||
export type ComponentOptionsMixin = ComponentOptionsBase<
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>
|
||||
|
||||
export type ComputedOptions = Record<
|
||||
string,
|
||||
ComputedGetter<any> | WritableComputedOptions<any>
|
||||
@@ -222,7 +277,9 @@ interface LegacyOptions<
|
||||
Props,
|
||||
D,
|
||||
C extends ComputedOptions,
|
||||
M extends MethodOptions
|
||||
M extends MethodOptions,
|
||||
Mixin extends ComponentOptionsMixin,
|
||||
Extends extends ComponentOptionsMixin
|
||||
> {
|
||||
// allow any custom options
|
||||
[key: string]: any
|
||||
@@ -232,8 +289,8 @@ interface LegacyOptions<
|
||||
// since that leads to some sort of circular inference and breaks ThisType
|
||||
// for the entire component.
|
||||
data?: (
|
||||
this: ComponentPublicInstance<Props>,
|
||||
vm: ComponentPublicInstance<Props>
|
||||
this: CreateComponentPublicInstance<Props>,
|
||||
vm: CreateComponentPublicInstance<Props>
|
||||
) => D
|
||||
computed?: C
|
||||
methods?: M
|
||||
@@ -242,8 +299,8 @@ interface LegacyOptions<
|
||||
inject?: ComponentInjectOptions
|
||||
|
||||
// composition
|
||||
mixins?: ComponentOptions[]
|
||||
extends?: ComponentOptions
|
||||
mixins?: Mixin[]
|
||||
extends?: Extends
|
||||
|
||||
// lifecycle
|
||||
beforeCreate?(): void
|
||||
@@ -261,6 +318,22 @@ interface LegacyOptions<
|
||||
errorCaptured?: ErrorCapturedHook
|
||||
}
|
||||
|
||||
export type OptionTypesKeys = 'P' | 'B' | 'D' | 'C' | 'M'
|
||||
|
||||
export type OptionTypesType<
|
||||
P = {},
|
||||
B = {},
|
||||
D = {},
|
||||
C extends ComputedOptions = {},
|
||||
M extends MethodOptions = {}
|
||||
> = {
|
||||
P: P
|
||||
B: B
|
||||
D: D
|
||||
C: C
|
||||
M: M
|
||||
}
|
||||
|
||||
const enum OptionTypes {
|
||||
PROPS = 'Props',
|
||||
DATA = 'Data',
|
||||
|
||||
Reference in New Issue
Block a user