Skip to content

Commit 6cdc99b

Browse files
HerringtonDarkholmeyyx990803
authored andcommitted
Recommend return type annotation (#1239)
* Recommend return type annotation * make explanation clear, thank @DanielRosenwasser
1 parent fac0af6 commit 6cdc99b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/v2/guide/typescript.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,39 @@ var vm = new Vue({
141141
myOption: 'Hello'
142142
})
143143
```
144+
145+
## Annotating Return Types
146+
147+
Because of the circular nature of Vue's declaration files, TypeScript may have difficulties inferring the types of certain methods.
148+
For this reason, you may need to annotate the return type on methods like `render` and those in `computed`.
149+
150+
```ts
151+
import Vue, { VNode } from 'vue'
152+
153+
const Component = Vue.extend({
154+
data() {
155+
return {
156+
msg: 'Hello'
157+
}
158+
},
159+
methods: {
160+
// need annotation due to `this` in return type
161+
greet(): string {
162+
return this.msg + ' world'
163+
}
164+
},
165+
computed: {
166+
// need annotation
167+
greeting(): string {
168+
return this.greet() + '!'
169+
}
170+
},
171+
// `createElement` is inferred, but `render` needs return type
172+
render(createElement): VNode {
173+
return createElement('div', this.greeting)
174+
}
175+
})
176+
```
177+
178+
If you find type inference or member completion isn't working, annotating certain methods may help address these problems.
179+
Using the `--noImplicitAny` option will help find many of these unannotated methods.

0 commit comments

Comments
 (0)