Update ComponentOptions (#3651)

* Update ComponentOptions

* Make ComponentOptions as a Generic Interface
* Update some interfaces and types related to ComponentOptions
* Update tests

* Fix
This commit is contained in:
Kaorun343 2016-09-14 08:59:14 +09:00 committed by Evan You
parent 43211fd519
commit 2b588b754e
4 changed files with 42 additions and 36 deletions

6
types/index.d.ts vendored
View File

@ -6,12 +6,12 @@ import * as VNode from "./vnode";
// `Vue` in `export = Vue` must be a namespace // `Vue` in `export = Vue` must be a namespace
// All available types are exported via this namespace // All available types are exported via this namespace
declare namespace Vue { declare namespace Vue {
export type ComponentOptions = Options.ComponentOptions; export type ComponentOptions<V extends Vue> = Options.ComponentOptions<V>;
export type FunctionalComponentOptions = Options.FunctionalComponentOptions; export type FunctionalComponentOptions = Options.FunctionalComponentOptions;
export type RenderContext = Options.RenderContext; export type RenderContext = Options.RenderContext;
export type PropOptions = Options.PropOptions; export type PropOptions = Options.PropOptions;
export type ComputedOptions = Options.ComputedOptions; export type ComputedOptions<V extends Vue> = Options.ComputedOptions<V>;
export type WatchHandler = Options.WatchHandler; export type WatchHandler<V extends Vue> = Options.WatchHandler<V>;
export type WatchOptions = Options.WatchOptions; export type WatchOptions = Options.WatchOptions;
export type DirectiveFunction = Options.DirectiveFunction; export type DirectiveFunction = Options.DirectiveFunction;
export type DirectiveOptions = Options.DirectiveOptions; export type DirectiveOptions = Options.DirectiveOptions;

42
types/options.d.ts vendored
View File

@ -7,37 +7,37 @@ type Constructor = {
type $createElement = typeof Vue.prototype.$createElement; type $createElement = typeof Vue.prototype.$createElement;
export interface ComponentOptions { export interface ComponentOptions<V extends Vue> {
data?: Object | ( (this: Vue) => Object ); data?: Object | ((this: V) => Object);
props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] }; props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] };
propsData?: Object; propsData?: Object;
computed?: { [key: string]: ((this: Vue) => any) | ComputedOptions }; computed?: { [key: string]: ((this: V) => any) | ComputedOptions<V> };
methods?: { [key: string]: Function }; methods?: { [key: string]: Function };
watch?: { [key: string]: ({ handler: WatchHandler } & WatchOptions) | WatchHandler | string }; watch?: { [key: string]: ({ handler: WatchHandler<V> } & WatchOptions) | WatchHandler<V> | string };
el?: Element | String; el?: Element | String;
template?: string; template?: string;
render?(createElement: $createElement): VNode; render?(this: V, createElement: $createElement): VNode;
staticRenderFns?: (() => VNode)[]; staticRenderFns?: ((createElement: $createElement) => VNode)[];
beforeCreate?(): void; beforeCreate?(this: V): void;
created?(): void; created?(this: V): void;
beforeDestroy?(): void; beforeDestroy?(this: V): void;
destroyed?(): void; destroyed?(this: V): void;
beforeMount?(): void; beforeMount?(this: V): void;
mounted?(): void; mounted?(this: V): void;
beforeUpdate?(): void; beforeUpdate?(this: V): void;
updated?(): void; updated?(this: V): void;
directives?: { [key: string]: DirectiveOptions | DirectiveFunction }; directives?: { [key: string]: DirectiveOptions | DirectiveFunction };
components?: { [key: string]: ComponentOptions | FunctionalComponentOptions | typeof Vue }; components?: { [key: string]: ComponentOptions<Vue> | FunctionalComponentOptions | typeof Vue };
transitions?: { [key: string]: Object }; transitions?: { [key: string]: Object };
filters?: { [key: string]: Function }; filters?: { [key: string]: Function };
parent?: Vue; parent?: Vue;
mixins?: (ComponentOptions | typeof Vue)[]; mixins?: (ComponentOptions<Vue> | typeof Vue)[];
name?: string; name?: string;
extends?: ComponentOptions | typeof Vue; extends?: ComponentOptions<Vue> | typeof Vue;
delimiters?: [string, string]; delimiters?: [string, string];
} }
@ -63,13 +63,13 @@ export interface PropOptions {
validator?(value: any): boolean; validator?(value: any): boolean;
} }
export interface ComputedOptions { export interface ComputedOptions<V> {
get?(this: Vue): any; get?(this: V): any;
set?(this: Vue, value: any): void; set?(this: V, value: any): void;
cache?: boolean; cache?: boolean;
} }
export type WatchHandler = <T>(val: T, oldVal: T) => void; export type WatchHandler<V> = (this: V, val: any, oldVal: any) => void;
export interface WatchOptions { export interface WatchOptions {
deep?: boolean; deep?: boolean;

View File

@ -7,6 +7,8 @@ interface Component extends Vue {
Vue.component('component', { Vue.component('component', {
data() { data() {
this.$mount
this.a
return { return {
a: 1 a: 1
} }
@ -50,7 +52,9 @@ Vue.component('component', {
}, },
'b': 'someMethod', 'b': 'someMethod',
'c': { 'c': {
handler(val: number, oldval: number) {}, handler(val, oldVal) {
this.a = val
},
deep: true deep: true
} }
}, },
@ -91,7 +95,9 @@ Vue.component('component', {
}, },
staticRenderFns: [], staticRenderFns: [],
beforeCreate() {}, beforeCreate() {
this.a = 1;
},
created() {}, created() {},
beforeDestroy() {}, beforeDestroy() {},
destroyed() {}, destroyed() {},
@ -120,7 +126,7 @@ Vue.component('component', {
}, },
components: { components: {
a: Vue.component(""), a: Vue.component(""),
b: {} as ComponentOptions b: {} as ComponentOptions<Vue>
}, },
transitions: {}, transitions: {},
filters: { filters: {
@ -129,11 +135,11 @@ Vue.component('component', {
} }
}, },
parent: new Vue, parent: new Vue,
mixins: [Vue.component(""), ({} as ComponentOptions)], mixins: [Vue.component(""), ({} as ComponentOptions<Vue>)],
name: "Component", name: "Component",
extends: {} as ComponentOptions, extends: {} as ComponentOptions<Vue>,
delimiters: ["${", "}"] delimiters: ["${", "}"]
} as ComponentOptions); } as ComponentOptions<Component>);
Vue.component('functional-component', { Vue.component('functional-component', {
props: ['prop'], props: ['prop'],

12
types/vue.d.ts vendored
View File

@ -11,11 +11,11 @@ import { PluginFunction, PluginObject } from "./plugin";
export declare class Vue { export declare class Vue {
constructor(options?: ComponentOptions); constructor(options?: ComponentOptions<Vue>);
$data: Object; $data: Object;
readonly $el: HTMLElement; readonly $el: HTMLElement;
readonly $options: ComponentOptions; readonly $options: ComponentOptions<this>;
readonly $parent: Vue; readonly $parent: Vue;
readonly $root: Vue; readonly $root: Vue;
readonly $children: Vue[]; readonly $children: Vue[];
@ -30,7 +30,7 @@ export declare class Vue {
$delete: typeof Vue.delete; $delete: typeof Vue.delete;
$watch( $watch(
expOrFn: string | Function, expOrFn: string | Function,
callback: WatchHandler, callback: WatchHandler<this>,
options?: WatchOptions options?: WatchOptions
): (() => void); ): (() => void);
$on(event: string, callback: Function): this; $on(event: string, callback: Function): this;
@ -54,7 +54,7 @@ export declare class Vue {
keyCodes: { [key: string]: number }; keyCodes: { [key: string]: number };
} }
static extend(options: ComponentOptions): typeof Vue; static extend(options: ComponentOptions<Vue>): typeof Vue;
static nextTick(callback: () => void, context?: any[]): void; static nextTick(callback: () => void, context?: any[]): void;
static set<T>(object: Object, key: string, value: T): T; static set<T>(object: Object, key: string, value: T): T;
static set<T>(array: T[], key: number, value: T): T; static set<T>(array: T[], key: number, value: T): T;
@ -67,11 +67,11 @@ export declare class Vue {
static filter(id: string, definition?: Function): Function; static filter(id: string, definition?: Function): Function;
static component( static component(
id: string, id: string,
definition?: ComponentOptions | FunctionalComponentOptions | typeof Vue definition?: ComponentOptions<Vue> | FunctionalComponentOptions | typeof Vue
): typeof Vue; ): typeof Vue;
static use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): void; static use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): void;
static mixin(mixin: typeof Vue | ComponentOptions): void; static mixin(mixin: typeof Vue | ComponentOptions<Vue>): void;
static compile(template: string): { static compile(template: string): {
render(createElement: typeof Vue.prototype.$createElement): VNode; render(createElement: typeof Vue.prototype.$createElement): VNode;
staticRenderFns: (() => VNode)[]; staticRenderFns: (() => VNode)[];