feat(types): expose ComponentCustomOptions for declaring custom options

This commit is contained in:
Evan You 2020-04-17 09:41:36 -04:00
parent be21cfb1db
commit c0adb67c2e
3 changed files with 35 additions and 4 deletions

View File

@ -51,6 +51,24 @@ import { Directive } from './directives'
import { ComponentPublicInstance } from './componentProxy' import { ComponentPublicInstance } from './componentProxy'
import { warn } from './warning' import { warn } from './warning'
/**
* Interface for declaring custom options.
*
* @example
* ```ts
* declare module '@vue/runtime-core' {
* interface ComponentCustomOptions {
* beforeRouteUpdate?(
* to: Route,
* from: Route,
* next: () => void
* ): void
* }
* }
* ```
*/
export interface ComponentCustomOptions {}
export interface ComponentOptionsBase< export interface ComponentOptionsBase<
Props, Props,
RawBindings, RawBindings,
@ -59,7 +77,10 @@ export interface ComponentOptionsBase<
M extends MethodOptions, M extends MethodOptions,
E extends EmitsOptions, E extends EmitsOptions,
EE extends string = string EE extends string = string
> extends LegacyOptions<Props, D, C, M>, SFCInternalOptions { >
extends LegacyOptions<Props, D, C, M>,
SFCInternalOptions,
ComponentCustomOptions {
setup?: ( setup?: (
this: void, this: void,
props: Props, props: Props,

View File

@ -189,8 +189,9 @@ export {
export { export {
ComponentOptions, ComponentOptions,
ComponentOptionsWithoutProps, ComponentOptionsWithoutProps,
ComponentOptionsWithObjectProps as ComponentOptionsWithProps, ComponentOptionsWithObjectProps,
ComponentOptionsWithArrayProps ComponentOptionsWithArrayProps,
ComponentCustomOptions
} from './componentOptions' } from './componentOptions'
export { export {
ComponentPublicInstance, ComponentPublicInstance,

View File

@ -1,7 +1,11 @@
import { expectError } from 'tsd' import { expectError, expectType } from 'tsd'
import { defineComponent } from './index' import { defineComponent } from './index'
declare module '@vue/runtime-core' { declare module '@vue/runtime-core' {
interface ComponentCustomOptions {
test?(n: number): void
}
interface ComponentCustomProperties { interface ComponentCustomProperties {
state: 'stopped' | 'running' state: 'stopped' | 'running'
} }
@ -9,6 +13,11 @@ declare module '@vue/runtime-core' {
export const Custom = defineComponent({ export const Custom = defineComponent({
data: () => ({ counter: 0 }), data: () => ({ counter: 0 }),
test(n) {
expectType<number>(n)
},
methods: { methods: {
aMethod() { aMethod() {
expectError(this.notExisting) expectError(this.notExisting)