You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/guide/typescript.md
+55-18Lines changed: 55 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -4,47 +4,84 @@ type: guide
4
4
order: 25
5
5
---
6
6
7
-
## Official Declaration Files
7
+
## Important 2.2 Change Notice for TS + webpack 2 users
8
8
9
-
A static type system can help prevent many potential runtime errors, especially as applications grow. That's why Vue ships with [official type declarations](https://github.com/vuejs/vue/tree/dev/types) for [TypeScript](https://www.typescriptlang.org/) - not only in Vue core, but also [for Vue Router](https://github.com/vuejs/vue-router/tree/dev/types) and [for Vuex](https://github.com/vuejs/vuex/tree/dev/types) as well.
9
+
In Vue 2.2 we introduced dist files exposed as ES modules, which will be used by default by webpack 2. Unfortunately, this introduced an unintentional breaking change because with TypeScript + webpack 2, `import Vue = require('vue')` will now return a synthetic ES module object instead of Vue itself.
10
10
11
-
Since these are [published on NPM](https://unpkg.com/vue/types/), you don't even need external tools like `Typings`, as declarations are automatically imported with Vue. That means all you need is a simple:
11
+
We plan to move all official declarations to use ES-style exports in the future. Please see [Recommended Configuration](#recommended-configuration) below on a future-proof setup.
12
12
13
-
```ts
14
-
importVue=require('vue')
13
+
## Official Declaration in NPM Packages
14
+
15
+
A static type system can help prevent many potential runtime errors, especially as applications grow. That's why Vue ships with [official type declarations](https://github.com/vuejs/vue/tree/dev/types) for [TypeScript](https://www.typescriptlang.org/) - not only in Vue core, but also for [vue-router](https://github.com/vuejs/vue-router/tree/dev/types) and [vuex](https://github.com/vuejs/vuex/tree/dev/types) as well.
16
+
17
+
Since these are [published on NPM](https://unpkg.com/vue/types/), and the latest TypeScript knows how to resolve type declarations in NPM packages, this means when installed via NPM, you don't need any additional tooling to use TypeScript with Vue.
18
+
19
+
## Recommended Configuration
20
+
21
+
```js
22
+
// tsconfig.json
23
+
{
24
+
"compilerOptions": {
25
+
// ... other options omitted
26
+
"allowSyntheticDefaultImports":true,
27
+
"lib": [
28
+
"dom",
29
+
"es5",
30
+
"es2015.Promise"
31
+
]
32
+
}
33
+
}
34
+
```
35
+
36
+
Note the `allowSyntheticDefaultImports` option allows us to use the following:
37
+
38
+
```js
39
+
importVuefrom'vue'
15
40
```
16
41
17
-
<pclass="tip">In Vue 2.2.0 we introduced dist files exposed as ES modules, which will be used by default by webpack 2. However, this means if you are using TypeScript with webpack 2, `import Vue = require('vue')` will return an ES module object instead of Vue itself.
42
+
instead of:
18
43
19
-
In the near future, we will update all core typings to use ES-style exports so there usage will be identical to importing ES modules; before these changes land, the temporary workaround is configuring webpack to alias `vue` back to `vue/dist/vue[.runtime].common.js`. You will also need to do the same for `vue-router` and `vuex` if you are using them.</p>
44
+
```js
45
+
import Vue = require('vue')
46
+
```
20
47
21
-
Then all methods, properties, and parameters will be type checked. For example, if you misspell the `template` component option as `tempate` (missing the `l`), the TypeScript compiler will print an error message at compile time. If you're using an editor that can lint TypeScript, such as [Visual Studio Code](https://code.visualstudio.com/), you'll even be able to catch these errors before compilation:
48
+
The former (ES module syntax) is recommended because it is consistent with recommended plain ES usage, and in the future we are planning to move all official declarations to use ES-style exports.
22
49
23
-

50
+
In addition, if you are using TypeScript with webpack 2, the following is also recommended:
51
+
52
+
```js
53
+
{
54
+
"compilerOptions": {
55
+
// ... other options omitted
56
+
"module":"es2015",
57
+
"moduleResolution":"node"
58
+
}
59
+
}
60
+
```
24
61
25
-
### Compilation Options
62
+
This tells TypeScript to leave the ES module import statements intact, which in turn allows webpack 2 to take advantage of ES-module-based tree-shaking.
26
63
27
-
Vue's declaration files require the `--lib DOM,ES5,ES2015.Promise`[compiler option](https://www.typescriptlang.org/docs/handbook/compiler-options.html). You can pass this option to the `tsc` command or add the equivalent to a `tsconfig.json` file.
64
+
See [TypeScript compiler options docs](https://www.typescriptlang.org/docs/handbook/compiler-options.html) for more details.
28
65
29
-
### Accessing Vue's Type Declarations
66
+
##Using Vue's Type Declarations
30
67
31
-
If you want to annotate your own code with Vue's types, you can access them on Vue's exported object. For example, to annotate an exported component options object (e.g. in a `.vue` file):
68
+
Vue's type definition exports many useful [type declarations](https://github.com/vuejs/vue/blob/dev/types/index.d.ts). For example, to annotate an exported component options object (e.g. in a `.vue` file):
32
69
33
70
```ts
34
-
importVue=require('vue')
71
+
importVue, { ComponentOptions } from'vue'
35
72
36
73
exportdefault {
37
74
props: ['message'],
38
75
template: '<span>{{ message }}</span>'
39
-
} asVue.ComponentOptions<Vue>
76
+
} asComponentOptions<Vue>
40
77
```
41
78
42
79
## Class-Style Vue Components
43
80
44
81
Vue component options can easily be annotated with types:
45
82
46
83
```ts
47
-
importVue=require('vue')
84
+
importVue, { ComponentOptions } from'vue'
48
85
49
86
// Declare the component's type
50
87
interfaceMyComponentextendsVue {
@@ -68,7 +105,7 @@ export default {
68
105
}
69
106
// We need to explicitly annotate the exported options object
70
107
// with the MyComponent type
71
-
} asVue.ComponentOptions<MyComponent>
108
+
} asComponentOptions<MyComponent>
72
109
```
73
110
74
111
Unfortunately, there are a few limitations here:
@@ -80,7 +117,7 @@ Unfortunately, there are a few limitations here:
80
117
Fortunately, [vue-class-component](https://github.com/vuejs/vue-class-component) can solve both of these problems. It's an official companion library that allows you to declare components as native JavaScript classes, with a `@Component` decorator. As an example, let's rewrite the above component:
81
118
82
119
```ts
83
-
importVue=require('vue')
120
+
importVuefrom'vue'
84
121
importComponentfrom'vue-class-component'
85
122
86
123
// The @Component decorator indicates the class is a Vue component
0 commit comments