Skip to content

Commit 0ca5870

Browse files
committed
update TypeScript support recommendations
1 parent 2207d5e commit 0ca5870

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

src/v2/guide/typescript.md

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,84 @@ type: guide
44
order: 25
55
---
66

7-
## Official Declaration Files
7+
## Important 2.2 Change Notice for TS + webpack 2 users
88

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.
1010

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.
1212

13-
``` ts
14-
import Vue = 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+
import Vue from 'vue'
1540
```
1641

17-
<p class="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:
1843

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+
```
2047

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.
2249

23-
![TypeScript Type Error in Visual Studio Code](/images/typescript-type-error.png)
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+
```
2461

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.
2663

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.
2865

29-
### Accessing Vue's Type Declarations
66+
## Using Vue's Type Declarations
3067

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):
3269

3370
``` ts
34-
import Vue = require('vue')
71+
import Vue, { ComponentOptions } from 'vue'
3572

3673
export default {
3774
props: ['message'],
3875
template: '<span>{{ message }}</span>'
39-
} as Vue.ComponentOptions<Vue>
76+
} as ComponentOptions<Vue>
4077
```
4178

4279
## Class-Style Vue Components
4380

4481
Vue component options can easily be annotated with types:
4582

4683
``` ts
47-
import Vue = require('vue')
84+
import Vue, { ComponentOptions } from 'vue'
4885

4986
// Declare the component's type
5087
interface MyComponent extends Vue {
@@ -68,7 +105,7 @@ export default {
68105
}
69106
// We need to explicitly annotate the exported options object
70107
// with the MyComponent type
71-
} as Vue.ComponentOptions<MyComponent>
108+
} as ComponentOptions<MyComponent>
72109
```
73110

74111
Unfortunately, there are a few limitations here:
@@ -80,7 +117,7 @@ Unfortunately, there are a few limitations here:
80117
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:
81118

82119
``` ts
83-
import Vue = require('vue')
120+
import Vue from 'vue'
84121
import Component from 'vue-class-component'
85122

86123
// The @Component decorator indicates the class is a Vue component

0 commit comments

Comments
 (0)