1. 程式人生 > >TypeScript學習筆記-高級類型

TypeScript學習筆記-高級類型

包含 類型別名 tor 一個 his 可選 -s 高級類型 -i

交叉類型為將兩個類型混合

聯合類型表示可以是幾種類型之一,用 | 分隔

如果一個值是聯合類型,那我們只能訪問次聯合類型的所有類型的公共成員

可以使用typeof檢查基礎類型和instanceof來詳細類型

typescript會把null和undefined區別對待

nul和undefined是所有其他類型的一個有效值

聲明了--strictNullChecks 標記後,當你聲明一個變量時,他不會自動的包含null和undefined。而可選參數和可選屬性會被自動的加上undefined

去除null的手段

(1)類型保護(prop ==null)

(2)斷言prop!.length

類型別名和接口的區別

(1)類型別名不能被extends和implements

/**
 * 交叉類型
 */
function extend<T,U>(first : T,second : U) : T&U {
  let result = <T&U>{};
  for (let id in first) {
    (<any>result)[id] = (<any>first)[id];
  }
  for(let id in second) {
    if(!result.hasOwnProperty(id)) {
      (<any>result)[id] = (<any>second)[id];
    }
  }
  
return result; } /** * 聯合類型 */ interface Fish{ swim(); layEggs(); } interface Bird{ fly(); layEggs(); } function getSmallPet():Fish | Bird{ //.... } let pet=getSmallPet(); //只能訪問聯合類型中所有類型的公共成員 pet.swim(); pet.layEggs(); //要想讓它正常工作,需要使用類型斷言 if((<Fish>pet).swim){ (<Fish>pet).swim(); }
else{ (<Bird>pet).fly(); } //用戶自定義的類型保護 //pet is Fish 類型謂詞,paramName is Type這種形式 function isFish(pet: Fish|Bird): pet is Fish{ return (<Fish>pet).swim !== undefined; } /** * 類型別名 */ type Name = string; type NameResolve = () => string; type NameOrNameResolve = Name | NameResolve; function getName(n: NameOrNameResolve) : Name { if(typeof n === ‘string‘){ return n; }else{ return n(); } } /** * 字符串字面量類型 */ type Easing = "ease-in" | "ease-out" | "ease-in-out"; class ElementUI{ animated(dx: number,dy: number,easing:Easing){ if(easing === "ease-in"){ }else if(easing === "ease-in-out"){ }else if(easing === "ease-out"){ } } } let button = new ElementUI(); button.animated(0,0,"ease-in"); button.animated(0,0,"ease-in-out") /** * 可辨識聯合 */ interface Circle { kind: ‘circle‘; radius: number; } interface Square { kind: ‘square‘; size: number; } interface Rectangle { kind: ‘rectangle‘; width: number; height: number; } type Shape = Circle | Square | Rectangle ; function computed(shape:Shape):number{ switch(shape.kind){ case ‘circle‘:return Math.PI*shape.radius**2; case ‘rectangle‘:return shape.width*shape.height; case ‘square‘:return shape.size*shape.size; } } /** * 多態的this類型 */ class BasicCaculate{ public constructor(protected value : number = 0){} public currentValue() : number { return this.value; } public add(operand : number) : this { this.value+=operand; return this; } public multiply(operand : number) : this { this.value*=operand; return this; } } let v=new BasicCaculate(2).multiply(1).add(5).currentValue(); //繼承 class ScientificCalculate extends BasicCaculate{ public constructor(value = 0){ super(value); } public sin() : this{ this.value=Math.sin(this.value); return this; } } let s=new ScientificCalculate(4).multiply(2).add(3).sin().currentValue(); /** * 索引類型 * key0f T 索引類型查詢操作符 * 對於任何T類型,keyof T的結果為T上已知的公共屬性名的聯合 */ function plunk<T,K extends keyof T>(o:T,names:K[]): T[K][]{ return names.map(n => o[n]); } interface Person{ name:String; age:number; } let person:Person = { name:‘edward‘, age:23 }; let strings:String[] = plunk(person,[‘name‘]); function getProperty<T,K extends keyof T>(o:T,names:K):T[K]{ return o[names]; } /** * 映射類型 * K in P,與索引類型一樣,內部使用了for...in */ type Keys=‘option1‘ | ‘option2‘; type Flags={[K in Keys]:boolean}; type ReadOnly<T> = { readonly [P in keyof T] : T[P]; } type Partials<T> = { [P in keyof T]? : T[P]; } //包裝類型 type Proxy<T> = { get():T; set(value : T) : void; } type Proxify<T> = { [P in keyof T] : Proxy<T[P]>; } function proxify<T>(O : T) : Proxify<T> { } //拆包,反向推斷 function unproxify<T>(t : Proxify<T>) : T{ let result = {} as T; for (const k in t) { result[k] = t[k].get(); } return result; }

TypeScript學習筆記-高級類型