類型檢查, 擁抱es6,支持部分的esNext草案,直接編譯到原生js、引入新的語法糖html
TypeScript的設計目的應該是解決JavaScript的「痛點」:弱類型和沒有命名空間,致使很難模塊化,不適合開發大型程序。另外它還提供了一些語法糖來幫助你們更方便地實踐面向對象的編程。vue
typescript不只能夠約束咱們的編碼習慣,還能起到註釋的做用,當咱們看到一函數後咱們立馬就能知道這個函數的用法,須要傳什麼值,返回值是什麼類型一目瞭然,對大型項目的維護性有很大的提高。es6
答案是確定的,固然能夠在tsconfig.json的配置, noEmitONErrorvuex
還沒搞定typescript
// undefined和null是全部類型子類型,均可以賦值
let num: Symbol = undefined;ss
let num: number = undefined;
// undefined類型, 只能給undefined
let u: undefined = undefined;
let n: null = null;
複製代碼
// 在ts中,變量在聲明的時候,若是沒有定義其類型,會被識成默認類型
let str;
str = 'I am strgting';
str = 1024;
// 未定義類型,直接賦值
let num= 124;
// 等同於 let num:number = 124, 在後面代碼若是賦予num一個string會被報錯
複製代碼
//只能訪問可能屬性的共有屬性
function getLength(param: string| number) {
return param.length;
}
// 會報錯, 由於 length不是 sting和number類型的共有屬性
// 技巧--》 使用類型別名
type possibleType = string | number;
function getLength(param: possibleType) {
return param.length;
}
複製代碼
必選參數和可選參數的類型是動態屬性類型的子集,全部在動態屬性類型設置的時候要設置上全部類型npm
interface userinfo {
"memberId": number,
"uName": string,
"employeeId": string,
"datetime": string,
"platformList"?: Array<platform>,
[propName: string]: string | number | Array<platform> | undefined
}
複製代碼
注意點: 只讀屬性的約束力在於第一次給對象賦值的時候,而不是給屬性賦值的時候 readonly和 const的區別: const是變量, readonly是屬性編程
export interface ISRequest {
fetch(url: string, arg?: Object, callback?: Function): Promise<Object>;
}
export class SafeRequest implements ISRequest {
public async fetch(url: string, arg, callback?: Function): Promise<Object> {
return new Promise((resolve, reject) => {
})
}
複製代碼
interface NumberArray {
[index: any]: number
}
let numArr: NumberArray = [1, 2, 3]
複製代碼
function buildName(firstName: string, lastName?: string) {
}
複製代碼
// <類型>值
// 值 as 類型
複製代碼
當使用第三方庫時,咱們須要引用它的聲明文件,才能得到對應的代碼補全、接口提示等功能。json
在 npm 包的聲明文件中,使用 declare 再也不會聲明一個全局變量,而只會在當前文件中聲明一個局部變量。只有在聲明文件中使用 export 導出,而後在使用方 import 導入後,纔會應用到這些類型聲明。 ######declare global 使用 declare global 能夠在 npm 包或者 UMD 庫中擴展全局變量的類型數組
內置對象查詢--》點擊
ESMAScript提供了Boolean、Error、Date、RegExpbash
interface obj = {
param: Function
param: Promise
}
複製代碼
enum companyList= {1: 'aaa', 2: 'bbb'}
var companyList = {
1: 'aaa',
2: 'bbb',
aaa: 1,
bbb: 2
}
複製代碼
import Vue from 'vue';
import Component from 'vue-componet-class';
Component.resgisterHooks([
'beforeRouteEnter'
])
@Componnet({
props: {
},
components: {
}
})
export default class App extends Vue {
// aa = '';
// 類型推斷aa是個string, 後面aa只能賦值aa類型
// 因此最好使用先聲明後
// data
public tableModelItems: Array<any>;
constructor() {
super();
this.tableModelItems = [];
}
// computed
public get filterTableData() {
return this.tableData.filter((i: any) => i.refundStatus === 0).length
// 方法
// 聲明週期
// 此時須要路由函數的生命週期鉤子咋辦
beforeRouteEnterf() {
next() // 必定要寫,不然玩不下去,爲何?
}
}
}
複製代碼
- vue-property-decorator(依賴vue-component-class提供了更多了裝飾器,代碼更加顯示 )
- @Emit
- @Inject
- @Prop
- @Provide
- @Watch
- vuex-class(鏈接了vue和vuex)
```js
複製代碼
添加script的類型
<script lang="ts"></script>
<!--不然下面的類型報錯-->
複製代碼
聲明再掛載
<!--inject-->
import _Vue from 'vue'
import moment from "moment";
export default {
install(Vue: typeof _Vue, options: any) {
Vue.prototype.$moment = moment;
Vue.prototype.$log = () => {
console.log(new Date())
}
}
}
<!--types-->
import Vue from 'vue'
declare module 'vue/types/vue' {
interface Vue {
$moment: Function
$log: Function
}
}
複製代碼
TypeScript 默認只識別 .ts 文件,不識別 .vue 文件, 乖乖的寫 import Component from 'components/component.vue'
@emit("reset")
reset(role, this.formData){}
<!--此時報錯-->
複製代碼