Skip to content

Commit 8f4aab2

Browse files
Kaorun343yyx990803
Kaorun343
authored andcommitted
Fix definitons (vuejs#3591)
* Fix types * add “FunctionalComponentOptions” and “ContextObject” * fix and update types * Rename * Update vue.d.ts * Update options-test.ts
1 parent 4061c42 commit 8f4aab2

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

types/options.d.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Vue } from "./vue.d";
2-
import { VNode, VNodeDirective } from "./vnode.d";
2+
import { VNode, VNodeData, VNodeDirective } from "./vnode.d";
33

44
type Constructor = {
55
new (...args: any[]): any;
66
}
77

8+
type $createElement = typeof Vue.prototype.$createElement;
9+
810
export interface ComponentOptions {
911
data?: Object | ( (this: Vue) => Object );
1012
props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] };
@@ -15,7 +17,7 @@ export interface ComponentOptions {
1517

1618
el?: Element | String;
1719
template?: string;
18-
render?(createElement: typeof Vue.prototype.$createElement): VNode;
20+
render?(createElement: $createElement): VNode;
1921
staticRenderFns?: (() => VNode)[];
2022

2123
beforeCreate?(): void;
@@ -28,7 +30,7 @@ export interface ComponentOptions {
2830
updated?(): void;
2931

3032
directives?: { [key: string]: DirectiveOptions | DirectiveFunction };
31-
components?: { [key: string]: ComponentOptions | typeof Vue };
33+
components?: { [key: string]: ComponentOptions | FunctionalComponentOptions | typeof Vue };
3234
transitions?: { [key: string]: Object };
3335
filters?: { [key: string]: Function };
3436

@@ -39,6 +41,21 @@ export interface ComponentOptions {
3941
delimiters?: [string, string];
4042
}
4143

44+
export interface FunctionalComponentOptions {
45+
props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] };
46+
functional: boolean;
47+
render(this: never, createElement: $createElement, context: RenderContext): VNode;
48+
name?: string;
49+
}
50+
51+
export interface RenderContext {
52+
props: any;
53+
children: VNode[];
54+
slots: any;
55+
data: VNodeData;
56+
parent: Vue;
57+
}
58+
4259
export interface PropOptions {
4360
type?: Constructor | Constructor[] | null;
4461
required?: boolean;

types/test/options-test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Vue = require("../index.d");
22
import { ComponentOptions } from "../options.d";
3+
import { FunctionalComponentOptions } from "../options.d";
34

45
interface Component extends Vue {
56
a: number;
67
}
78

8-
const Options: ComponentOptions = {
9+
Vue.component('component', {
910
data() {
1011
return {
1112
a: 1
@@ -133,4 +134,17 @@ const Options: ComponentOptions = {
133134
name: "Component",
134135
extends: {} as ComponentOptions,
135136
delimiters: ["${", "}"]
136-
}
137+
} as ComponentOptions);
138+
139+
Vue.component('functional-component', {
140+
props: ['prop'],
141+
functional: true,
142+
render(createElement, context) {
143+
context.props;
144+
context.children;
145+
context.slots;
146+
context.data;
147+
context.parent;
148+
return createElement("div", {}, context.children);
149+
}
150+
} as FunctionalComponentOptions);

types/vue.d.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
ComponentOptions,
3+
FunctionalComponentOptions,
34
WatchOptions,
45
WatchHandler,
56
DirectiveOptions,
@@ -59,11 +60,20 @@ export declare class Vue {
5960
static set<T>(array: T[], key: number, value: T): T;
6061
static delete(object: Object, key: string): void;
6162

62-
static directive(id: string, definition?: DirectiveOptions | DirectiveFunction): DirectiveOptions;
63+
static directive(
64+
id: string,
65+
definition?: DirectiveOptions | DirectiveFunction
66+
): DirectiveOptions;
6367
static filter(id: string, definition?: Function): Function;
64-
static component(id: string, definition?: ComponentOptions | typeof Vue): typeof Vue;
68+
static component(
69+
id: string,
70+
definition?: ComponentOptions | FunctionalComponentOptions | typeof Vue
71+
): typeof Vue;
6572

6673
static use<T>(plugin: PluginObject<T> | PluginFunction<T>, options?: T): void;
6774
static mixin(mixin: typeof Vue | ComponentOptions): void;
68-
static compile(template: string): { render: Function, staticRenderFns: Function };
75+
static compile(template: string): {
76+
render(createElement: typeof Vue.prototype.$createElement): VNode;
77+
staticRenderFns: (() => VNode)[];
78+
};
6979
}

0 commit comments

Comments
 (0)