diff --git a/src/v2/guide/typescript.md b/src/v2/guide/typescript.md index e7d9b61f00..8dddc833d4 100644 --- a/src/v2/guide/typescript.md +++ b/src/v2/guide/typescript.md @@ -139,3 +139,39 @@ var vm = new Vue({ myOption: 'Hello' }) ``` + +## Annotating Return Types + +Because of the circular nature of Vue's declaration files, TypeScript may have difficulties inferring the types of certain methods. +For this reason, you may need to annotate the return type on methods like `render` and those in `computed`. + +```ts +import Vue, { VNode } from 'vue' + +const Component = Vue.extend({ + data() { + return { + msg: 'Hello' + } + }, + methods: { + // need annotation due to `this` in return type + greet(): string { + return this.msg + ' world' + } + }, + computed: { + // need annotation + greeting(): string { + return this.greet() + '!' + } + }, + // `createElement` is inferred, but `render` needs return type + render(createElement): VNode { + return createElement('div', this.greeting) + } +}) +``` + +If you find type inference or member completion isn't working, annotating certain methods may help address these problems. +Using the `--noImplicitAny` option will help find many of these unannotated methods.