android 向系統日曆新增日程事件
阿新 • • 發佈:2020-11-23
一:基礎語法庫
1.靜態型別的深度理解
const count : number = 123 //count就具備了number型別的方法
2.型別
①:基礎型別:number、string、null、undefined、symbol、boolean、void、bigint
②:物件型別
//陣列 const numbers: number[] = [1,2,3] //元組 (tuple 和陣列非常相似,只是把型別寫著數組裡面,少寫或者多寫都會報錯 )可以呼叫push 但是push的型別只能是已經定義過的 let user:[string,number] = [18,'alhh'] //物件 const teacher:{ name:string }//類 class Person{} const lqq:Person = new Person
3.interface介面
對物件的形狀(shape)進行描述,Duck Typing(鴨子型別)
interface Person { name:string, age?:number; } let alhh:Person = { name:'lqq' }
4.函式
//約定輸入,約定輸出 可選引數後面不能再跟確定的引數 function add(x:number,y:number,z?:number):number{ return a +b }
interface描述函式型別
const sum = (x: number, y: number) => { return x + y } interface ISum { (x:number,y:number):number } const sum2:ISum = sum
3.型別註解和型別推斷(type annotation & type inference)、聯合型別
①:型別註解
let count: number //告訴ts變數是什麼型別
②:型別推斷
let countInference = 123 //ts會自動的去嘗試分析變數的型別
③:聯合型別 ( | )不確定是number還是string
let user : numer | string = 'alhh'
④:型別守衛
在聯合型別中,不同的條件分支裡面,智慧的縮小範圍,程式碼出錯的機率就會降低(先判斷是某種型別然後去做某事)
如果ts能夠自動分析變數型別,就什麼也不用做了,如果無法分析變數型別就需要型別註解了
例如在函式傳參的時候 需要型別註解引數型別
4. class (封裝、繼承、多型)
①:抽象類只能繼承不能例項化。他把共用的東西抽出來封裝
ts介面也可以將通用的提煉出來,然後extends
protected:它在子類中式允許被訪問的
②:類和介面
interface Radio { switchRadio(trigger:boolean):void } interface Battery { checkBatteryStatus(): void; } class Car implements Radio{ switchRadio(trigger:boolean){ } } class Cellphone implements Radio,Battery{ switchRadio(trigger:boolean){ }, checkBatteryStatus(): { }; } //或者 用一個介面繼承Radio 然後cellphone去實現它 interface RadioWithBattery extends Radio{ checkBatteryStatus():void } class Cellphone implements RadioWithBattery{ switchRadio(trigger:boolean){ }, checkBatteryStatus(): { }; }
5.列舉(enum)
enum Direction{ Up, Down, Right, Left } console.log(Direction.up) //0 console.log(Direction[0]) //編譯後 (function(Direction){ Direction[Direction["up"]=0] ="up" Direction[Direction["Down"]=1] ="Down" }(Direction||Direction={}))
6.泛型(Generics)
①:在定義介面、函式和類的時候,不預先指定具體的型別,而在使用的時候指定型別
function echo<T>(arg:T):T { return arg } //可以傳多個值 (交換值) function swap<T,U>(tuple:[T,U]):[U,T]{ return [tuple[1],tuple[0]] } const result = swap(['string', 123])
②:泛型約束
在函式內部使用泛型變數時候,由於不知道是哪種型別,所以不能隨意操作他的屬性和方法
interface INumber{ length:number } function echoWithLength<T extends INumber>(arg:T):T{ return arg.length }
echoWithLength('str')
const result3 = echoWithLength({length: 10})
const result4 = echoWithLength([1, 2, 3])
③:泛型與類和介面
//類 class Queue<T>{ private data = [] push(item:T){ return this.data.push(item) } pop():T{ this.data.shift() } } const queue = new Queue() queue.push(1) queue.push('str') console.log(queue.pop().toFixed()) //可以像陣列新增各種型別的值,但是在使用中就會出錯,只有number型別有toFixed方法 修改加泛型 <T> 在類和方法上加 T const queue = new Queue<number>() //介面 interface KeyPair<T,U>{ key:T, value:U }
④:類型別名,字面量和交叉型別
//類型別名 type PlusType = (x:number,y:number) =>number let sum:PlusType //字面量 const str:'name' = 'name' //等於name2都會報錯 const number:1 = 1 type Directions = 'Up' | 'Down' | 'left' | 'Right' let toWhere:Directions = 'Left' //交叉型別 interface IName{ name:string } type IPerson = IName & {age:number}
7.宣告檔案
declare var jQuery:(selector:string)=>any
8.內建型別
ts還提供了一些功能性,幫助性的型別,這些型別,在js世界是看不到的,叫utility type
//Partial可以把傳入的型別都變成可選 interface IPerson{ name:string, age:number } let IPartial = Partial<IPerson> //返回的型別可以忽略傳入型別的某個屬性 type IOmit = Omit<IPerson,'name'>