From 14d58d7642a534dc6fe74b86bd77ff1938140b4c Mon Sep 17 00:00:00 2001 From: kaorun343 Date: Sat, 3 Sep 2016 01:35:59 +0900 Subject: [PATCH 1/4] Fix types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add “FunctionalComponentOptions” and “ContextObject” * fix and update types --- types/options.d.ts | 23 ++++++++++++++++++++--- types/test/options-test.ts | 19 ++++++++++++++++--- types/vue.d.ts | 18 ++++++++++++++---- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/types/options.d.ts b/types/options.d.ts index 2216e4a751b..3557fc4c1b9 100644 --- a/types/options.d.ts +++ b/types/options.d.ts @@ -1,10 +1,12 @@ import { Vue } from "./vue.d"; -import { VNode, VNodeDirective } from "./vnode.d"; +import { VNode, VNodeData, VNodeDirective } from "./vnode.d"; type Constructor = { new (...args: any[]): any; } +type $createElement = typeof Vue.prototype.$createElement; + export interface ComponentOptions { data?: Object | ( (this: Vue) => Object ); props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] }; @@ -15,7 +17,7 @@ export interface ComponentOptions { el?: Element | String; template?: string; - render?(createElement: typeof Vue.prototype.$createElement): VNode; + render?(createElement: $createElement): VNode; staticRenderFns?: (() => VNode)[]; beforeCreate?(): void; @@ -28,7 +30,7 @@ export interface ComponentOptions { updated?(): void; directives?: { [key: string]: DirectiveOptions | DirectiveFunction }; - components?: { [key: string]: ComponentOptions | typeof Vue }; + components?: { [key: string]: ComponentOptions | FunctionalComponentOptions | typeof Vue }; transitions?: { [key: string]: Object }; filters?: { [key: string]: Function }; @@ -39,6 +41,21 @@ export interface ComponentOptions { delimiters?: [string, string]; } +export interface FunctionalComponentOptions { + props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] }; + functional: boolean; + render(this: never, createElement: $createElement, context: ContextObject): VNode; + name?: string; +} + +export interface ContextObject { + props: any; + children: VNode[]; + slots: any; + data: VNodeData; + parent: Vue; +} + export interface PropOptions { type?: Constructor | Constructor[] | null; required?: boolean; diff --git a/types/test/options-test.ts b/types/test/options-test.ts index 23af5cde7aa..326bc861496 100644 --- a/types/test/options-test.ts +++ b/types/test/options-test.ts @@ -1,11 +1,11 @@ import { Vue } from "../vue.d"; -import { ComponentOptions } from "../options.d"; +import { ComponentOptions, FunctionalComponentOptions } from "../options.d"; interface Component extends Vue { a: number; } -const Options: ComponentOptions = { +Vue.component('component', { data() { return { a: 1 @@ -133,4 +133,17 @@ const Options: ComponentOptions = { name: "Component", extends: {} as ComponentOptions, delimiters: ["${", "}"] -} +} as ComponentOptions); + +Vue.component('functional-component', { + props: ['prop'], + functional: true, + render(createElement, context) { + context.props; + context.children; + context.slots; + context.data; + context.parent; + return createElement("div", {}, context.children); + } +} as FunctionalComponentOptions); diff --git a/types/vue.d.ts b/types/vue.d.ts index 449735d3ef7..847640a5419 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -1,5 +1,6 @@ import { ComponentOptions, + FunctionalComponentOptions, WatchOptions, WatchHandler, DirectiveOptions, @@ -53,17 +54,26 @@ export declare class Vue { keyCodes: { [key: string]: number }; } - static extend(options: ComponentOptions): Vue; + static extend(options: ComponentOptions): typeof Vue; static nextTick(callback: () => void, context?: any[]): void; static set(object: Object, key: string, value: T): T; static set(array: T[], key: number, value: T): T; static delete(object: Object, key: string): void; - static directive(id: string, definition?: DirectiveOptions | DirectiveFunction): DirectiveOptions; + static directive( + id: string, + definition?: DirectiveOptions | DirectiveFunction + ): DirectiveOptions; static filter(id: string, definition?: Function): Function; - static component(id: string, definition?: ComponentOptions | typeof Vue): typeof Vue; + static component( + id: string, + definition?: ComponentOptions | FunctionalComponentOptions | typeof Vue + ): typeof Vue; static use(plugin: PluginObject | PluginFunction, options?: T): void; static mixin(mixin: typeof Vue | ComponentOptions): void; - static compile(template: string): { render: Function, staticRenderFns: Function }; + static compile(template: string): { + render(createElement: typeof Vue.prototype.$createElement): VNode; + staticRenderFns: (() => VNode)[]; + }; } From d00882c9bfef208bedbdb38c7129a783bfbd8538 Mon Sep 17 00:00:00 2001 From: kaorun343 Date: Sat, 3 Sep 2016 05:05:09 +0900 Subject: [PATCH 2/4] Rename --- types/options.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/options.d.ts b/types/options.d.ts index 3557fc4c1b9..efa536cb964 100644 --- a/types/options.d.ts +++ b/types/options.d.ts @@ -44,11 +44,11 @@ export interface ComponentOptions { export interface FunctionalComponentOptions { props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] }; functional: boolean; - render(this: never, createElement: $createElement, context: ContextObject): VNode; + render(this: never, createElement: $createElement, context: RenderContext): VNode; name?: string; } -export interface ContextObject { +export interface RenderContext { props: any; children: VNode[]; slots: any; From c120ae14f1580e5db821f188385a8bbee988f60f Mon Sep 17 00:00:00 2001 From: Kaorun343 Date: Sun, 4 Sep 2016 21:22:17 +0900 Subject: [PATCH 3/4] Update vue.d.ts --- types/vue.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/vue.d.ts b/types/vue.d.ts index 847640a5419..ce1ead00fa8 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -54,7 +54,7 @@ export declare class Vue { keyCodes: { [key: string]: number }; } - static extend(options: ComponentOptions): typeof Vue; + static extend(options: ComponentOptions): Vue; static nextTick(callback: () => void, context?: any[]): void; static set(object: Object, key: string, value: T): T; static set(array: T[], key: number, value: T): T; From 2c79b8fafac7cdccfa36b386c370986f1a3be1d9 Mon Sep 17 00:00:00 2001 From: Kaorun343 Date: Sun, 4 Sep 2016 21:31:17 +0900 Subject: [PATCH 4/4] Update options-test.ts --- types/test/options-test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/test/options-test.ts b/types/test/options-test.ts index 326bc861496..5737b07df23 100644 --- a/types/test/options-test.ts +++ b/types/test/options-test.ts @@ -1,5 +1,6 @@ import { Vue } from "../vue.d"; -import { ComponentOptions, FunctionalComponentOptions } from "../options.d"; +import { ComponentOptions } from "../options.d"; +import { FunctionalComponentOptions } from "../options.d"; interface Component extends Vue { a: number;