- Há»c Vue
-
HỠsinh thái
HỠtrợ
Công cụ
ThÆ° viá»n chÃnh thức
Tin tức
Tà i nguyên
- Äá»i ngÅ©
- HỠtrợ Vue
- Ngôn ngữ
HÆ°á»ng dẫn
Khái niá»m cÆ¡ bản
- Cà i Äặt
- Giá»i thiá»u
- Äá»i tượng Vue
- Cú pháp template
- Computed property và watcher
- Binding cho class và style
- Render theo Äiá»u kiá»n
- Render danh sách
- Xá» là sá»± kiá»n
- Rà ng buá»c form input
- Cơ bản vỠcomponent
Components In-Depth
- ÄÄng kà Component
- Props
- Custom Events
- Slots
- Dynamic & Async Components
- Handling Edge Cases
Hiá»u ứng chuyá»n Äá»ng
- Transition cho enter/leave & danh sách
- Transition cho trạng thái
Tái sỠdụng & kết hợp
- Mixin
- Directive tùy biến
- Các hà m render & JSX
- Plugin
- Filter
Công cụ
- Triá»n khai cho môi trÆ°á»ng production
- Single File Components
- Unit test
- TypeScript Support
Má» rá»ng quy mô ứng dụng
- Routing
- Quản là trạng thái
- Render á» phÃa server
Bên trong Vue
- Reactivity in Depth
Thông tin thêm
- Comparison with Other Frameworks
- Join the Vue.js Community!
- Äá»i ngÅ©
TypeScript Support
In Vue 2.5.0+ we have greatly improved our type declarations to work with the default object-based API. At the same time it introduces a few changes that require upgrade actions. Read this blog post for more details.
Official Declaration in NPM Packages
A static type system can help prevent many potential runtime errors, especially as applications grow. Thatâs why Vue ships with official type declarations for TypeScript - not only in Vue core, but also for vue-router and vuex as well.
Since these are published on NPM, 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.
We also plan to provide an option to scaffold a ready-to-go Vue + TypeScript project in vue-cli
in the near future.
Recommended Configuration
// tsconfig.json |
Note that you have to include strict: true
(or at least noImplicitThis: true
which is a part of strict
flag) to leverage type checking of this
in component methods otherwise it is always treated as any
type.
See TypeScript compiler options docs for more details.
Development Tooling
For developing Vue applications with TypeScript, we strongly recommend using Visual Studio Code, which provides great out-of-the-box support for TypeScript.
If you are using single-file components (SFCs), get the awesome Vetur extension, which provides TypeScript inference inside SFCs and many other great features.
WebStorm also provides out-of-the-box support for both TypeScript and Vue.js.
Basic Usage
To let TypeScript properly infer types inside Vue component options, you need to define components with Vue.component
or Vue.extend
:
import Vue from 'vue' |
Class-Style Vue Components
If you prefer a class-based API when declaring components, you can use the officially maintained vue-class-component decorator:
import Vue from 'vue' |
Augmenting Types for Use with Plugins
Plugins may add to Vueâs global/instance properties and component options. In these cases, type declarations are needed to make plugins compile in TypeScript. Fortunately, thereâs a TypeScript feature to augment existing types called module augmentation.
For example, to declare an instance property $myProperty
with type string
:
// 1. Make sure to import 'vue' before declaring augmented types |
After including the above code as a declaration file (like my-property.d.ts
) in your project, you can use $myProperty
on a Vue instance.
var vm = new Vue() |
You can also declare additional global properties and component options:
import Vue from 'vue' |
The above declarations allow the following code to be compiled:
// Global property |
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
.
import Vue, { VNode } from 'vue' |
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.