Skip to content

Commit

Permalink
feat(types): expose ComponentCustomOptions for declaring custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 17, 2020
1 parent be21cfb commit c0adb67
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
23 changes: 22 additions & 1 deletion packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ import { Directive } from './directives'
import { ComponentPublicInstance } from './componentProxy'
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<
Props,
RawBindings,
Expand All @@ -59,7 +77,10 @@ export interface ComponentOptionsBase<
M extends MethodOptions,
E extends EmitsOptions,
EE extends string = string
> extends LegacyOptions<Props, D, C, M>, SFCInternalOptions {
>
extends LegacyOptions<Props, D, C, M>,
SFCInternalOptions,
ComponentCustomOptions {
setup?: (
this: void,
props: Props,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ export {
export {
ComponentOptions,
ComponentOptionsWithoutProps,
ComponentOptionsWithObjectProps as ComponentOptionsWithProps,
ComponentOptionsWithArrayProps
ComponentOptionsWithObjectProps,
ComponentOptionsWithArrayProps,
ComponentCustomOptions
} from './componentOptions'
export {
ComponentPublicInstance,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { expectError } from 'tsd'
import { expectError, expectType } from 'tsd'
import { defineComponent } from './index'

declare module '@vue/runtime-core' {
interface ComponentCustomOptions {
test?(n: number): void
}

interface ComponentCustomProperties {
state: 'stopped' | 'running'
}
}

export const Custom = defineComponent({
data: () => ({ counter: 0 }),

test(n) {
expectType<number>(n)
},

methods: {
aMethod() {
expectError(this.notExisting)
Expand Down

0 comments on commit c0adb67

Please sign in to comment.