Skip to content

Commit 6643cb8

Browse files
ktsnyyx990803
authored andcommitted
Update typings (#3876)
* ensure `this` in methods is vue component * update createElement type and expose it
1 parent f35f7e3 commit 6643cb8

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

Diff for: types/index.d.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import {Vue as _Vue} from "./vue";
1+
import * as V from "./vue";
22
import * as Options from "./options";
33
import * as Plugin from "./plugin";
44
import * as VNode from "./vnode";
55

66
// `Vue` in `export = Vue` must be a namespace
77
// All available types are exported via this namespace
88
declare namespace Vue {
9+
export type CreateElement = V.CreateElement;
10+
911
export type Component = Options.Component;
1012
export type AsyncComponent = Options.AsyncComponent;
1113
export type ComponentOptions<V extends Vue> = Options.ComponentOptions<V>;
@@ -30,6 +32,6 @@ declare namespace Vue {
3032
}
3133

3234
// TS cannot merge imported class with namespace, declare a subclass to bypass
33-
declare class Vue extends _Vue {}
35+
declare class Vue extends V.Vue {}
3436

3537
export = Vue;

Diff for: types/options.d.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { Vue } from "./vue";
1+
import { Vue, CreateElement } from "./vue";
22
import { VNode, VNodeData, VNodeDirective } from "./vnode";
33

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

8-
type $createElement = typeof Vue.prototype.$createElement;
9-
108
export type Component = typeof Vue | ComponentOptions<Vue> | FunctionalComponentOptions;
119
export type AsyncComponent = (
1210
resolve: (component: Component) => void,
@@ -18,13 +16,13 @@ export interface ComponentOptions<V extends Vue> {
1816
props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] };
1917
propsData?: Object;
2018
computed?: { [key: string]: ((this: V) => any) | ComputedOptions<V> };
21-
methods?: { [key: string]: Function };
19+
methods?: { [key: string]: (this: V, ...args: any[]) => any };
2220
watch?: { [key: string]: ({ handler: WatchHandler<V> } & WatchOptions) | WatchHandler<V> | string };
2321

2422
el?: Element | String;
2523
template?: string;
26-
render?(this: V, createElement: $createElement): VNode;
27-
staticRenderFns?: ((createElement: $createElement) => VNode)[];
24+
render?(this: V, createElement: CreateElement): VNode;
25+
staticRenderFns?: ((createElement: CreateElement) => VNode)[];
2826

2927
beforeCreate?(this: V): void;
3028
created?(this: V): void;
@@ -50,7 +48,7 @@ export interface ComponentOptions<V extends Vue> {
5048
export interface FunctionalComponentOptions {
5149
props?: string[] | { [key: string]: PropOptions | Constructor | Constructor[] };
5250
functional: boolean;
53-
render(this: never, createElement: $createElement, context: RenderContext): VNode;
51+
render(this: never, createElement: CreateElement, context: RenderContext): VNode;
5452
name?: string;
5553
}
5654

Diff for: types/test/options-test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Vue.component('component', {
4242
}
4343
},
4444
methods: {
45-
plus(this: Component) {
45+
plus() {
4646
this.a++;
4747
}
4848
},
@@ -88,7 +88,8 @@ Vue.component('component', {
8888
key: 'myKey',
8989
ref: 'myRef'
9090
}, [
91-
createElement("div", {}, "message"),
91+
createElement(),
92+
createElement("div", "message"),
9293
createElement(Vue.component("component")),
9394
createElement({} as ComponentOptions<Vue>),
9495
createElement({ functional: true }),
@@ -107,7 +108,7 @@ Vue.component('component', {
107108

108109
"message",
109110

110-
[createElement("div", {}, "message")]
111+
[createElement("div", "message")]
111112
]);
112113
},
113114
staticRenderFns: [],

Diff for: types/vue.d.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ import {
1111
import { VNode, VNodeData, VNodeChildren } from "./vnode";
1212
import { PluginFunction, PluginObject } from "./plugin";
1313

14+
export type CreateElement = {
15+
// empty node
16+
(): VNode;
17+
18+
// element or component name
19+
(tag: string, children: VNodeChildren): VNode;
20+
(tag: string, data?: VNodeData, children?: VNodeChildren): VNode;
21+
22+
// component constructor or options
23+
(tag: Component, children: VNodeChildren): VNode;
24+
(tag: Component, data?: VNodeData, children?: VNodeChildren): VNode;
25+
26+
// async component
27+
(tag: AsyncComponent, children: VNodeChildren): VNode;
28+
(tag: AsyncComponent, data?: VNodeData, children?: VNodeChildren): VNode;
29+
}
30+
1431
export declare class Vue {
1532

1633
constructor(options?: ComponentOptions<Vue>);
@@ -40,12 +57,7 @@ export declare class Vue {
4057
$off(event?: string, callback?: Function): this;
4158
$emit(event: string, ...args: any[]): this;
4259
$nextTick(callback?: (this: this) => void): void;
43-
$createElement(
44-
tag?: string | Component | AsyncComponent,
45-
data?: VNodeData,
46-
children?: VNodeChildren
47-
): VNode;
48-
60+
$createElement: CreateElement;
4961

5062
static config: {
5163
silent: boolean;

0 commit comments

Comments
 (0)