最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

typescript中聲明合并介紹

 更新時(shí)間:2022年09月15日 10:42:02   作者:RadiumAg???????  
這篇文章主要介紹了typescript中聲明合并介紹,類型合并表明編譯器將合并兩個(gè)分開(kāi)的并且名稱相同的聲明,合并之后的聲明擁有兩個(gè)聲明的特點(diǎn),任意數(shù)量的聲明可以被合并,不僅限兩個(gè),更多相關(guān)內(nèi)容需要的朋友可以參考下面文章內(nèi)容

聲明合并

類型合并表明編譯器合并兩個(gè)分開(kāi)的并且名稱相同的聲明,合并之后的聲明擁有兩個(gè)聲明的特點(diǎn),任意數(shù)量的聲明可以被合并,不僅限兩個(gè)。

合并Interface

1.interface的非函數(shù)成員應(yīng)該是唯一的,如果兩個(gè)interface都聲明一個(gè)名稱相同但類型不同非函數(shù)成員,編譯器將提示錯(cuò)誤:

interface Box { 
   height: number; 
 }
interface Box {
   height: string; 
 }

2.對(duì)于函數(shù)成員,每個(gè)相同名稱的成員被看作是相同名稱函數(shù)的重載,但是當(dāng)出現(xiàn)兩個(gè)interface時(shí),第二個(gè)有更高的優(yōu)先級(jí),會(huì)覆蓋前一個(gè):

interface Cloner {
  clone(animal: Animal): Animal;
}

interface Cloner {
  clone(animal: Sheep): Sheep;
}

interface Cloner {
  clone(animal: Dog): Dog;
  clone(animal: Cat): Cat;
}

// 最終的排序是
interface Cloner {
  clone(animal: Dog): Dog;
  clone(animal: Cat): Cat;
  clone(animal: Sheep): Sheep;
  clone(animal: Animal): Animal;
}

當(dāng)然這個(gè)規(guī)則有一個(gè)例外,當(dāng)函數(shù)的參數(shù)類型是一個(gè)單字面量類型(single string literal type),它將會(huì)根據(jù)優(yōu)先級(jí)排序,并放在聲明頂部

interface Document {
  createElement(tagName: any): Element;
}

interface Document {
  createElement(tagName: 'div'): HTMLDivElement;
  createElement(tagName: 'span'): HTMLSpanElement;
}

interface Document {
  createElement(tagName: string): HTMLElement;
  createElement(tagName: 'canvas'): HTMLCanvasElement;
}

// 字面量根據(jù)冒泡排序并放在了聲明頂部
interface Document {
  createElement(tagName: 'canvas'): HTMLCanvasElement;
  createElement(tagName: 'div'): HTMLDivElement;
  createElement(tagName: 'span'): HTMLSpanElement;
  createElement(tagName: string): HTMLElement;
  createElement(tagName: any): Element;
}

合并Namespace

  • 合并兩個(gè)相同名稱的namespace時(shí),將進(jìn)一步添加第二個(gè)namespace導(dǎo)出的成員到第一個(gè)namespace。
namespace Animals {
  export class Zebra {}
}

namespace Animals {
  export interface Legged {
    numberOfLegs: number;
  }
  export class Dog {}
}

// 合并到了第一個(gè)
namespace Animals {
  export interface Legged {
    numberOfLegs: number;
  }
  export class Zebra {}
  export class Dog {}
}
  • 當(dāng)一個(gè)namespace發(fā)生合并時(shí),和它合并的namesapce不能訪問(wèn)它的未導(dǎo)出的成員
namespace Animal {
  const haveMuscles = true;
  export function animalsHaveMuscles() {
    return haveMuscles;
  }
}
namespace Animal {
  export function doAnimalsHaveMuscles() {
    return haveMuscles; // Error, because haveMuscles is not accessible here
  }
}

可以看到無(wú)法訪問(wèn)haveMuscles,同時(shí)運(yùn)行也會(huì)報(bào)錯(cuò),可以結(jié)合編譯后的例子看:

namespace和class、enum、function合并

  • 和合并namespace一樣,class可以訪問(wèn)namespace中導(dǎo)出的類型
class Album {
  label: Album.AlbumLabel;
}
namespace Album {
  export class AlbumLabel {}
}
  • namespacefunction合并可以像javascript那樣在方法上添加屬性:
function buildLabel(name: string): string {
  return buildLabel.prefix + name + buildLabel.suffix;
}
namespace buildLabel {
  export const suffix = '';
  export const prefix = 'Hello, ';
}
console.log(buildLabel('Sam Smith'));

可以看編譯之后的代碼,可以看到直接在buildLabel上添加了屬性:

  • namespaceenum發(fā)生合并時(shí),namespace可以擴(kuò)展enum
enum Color {
  red = 1,
  green = 2,
  blue = 4,
}
namespace Color {
  export function mixColor(colorName: string) {
    if (colorName == 'yellow') {
      return Color.red + Color.green;
    } else if (colorName == 'white') {
      return Color.red + Color.green + Color.blue;
    } else if (colorName == 'magenta') {
      return Color.red + Color.blue;
    } else if (colorName == 'cyan') {
      return Color.green + Color.blue;
    }
  }
}

可以看編譯之后的:

class之間不允許合并,但是如果需要模仿類似功能,可以參照 Mixins in Typscripts

Module擴(kuò)展

盡管Module之間是不支持合并的,但是你可以通過(guò)導(dǎo)入需要擴(kuò)展的方法,然后再更改它,這種方式去實(shí)現(xiàn):

// observable.ts
export class Observable<T> {
  // ... implementation left as an exercise for the reader ...
}

// map.ts
import { Observable } from "./observable";
Observable.prototype.map = function (f) {
  // ... another exercise for the reader
};

但是這樣編譯器并不能提供良好的提示,所以需要擴(kuò)展module聲明:

// observable.ts
export class Observable<T> {
  // ... implementation left as an exercise for the reader ...
}

// map.ts
import { Observable } from "./observable";
declare module "./observable" {
  interface Observable<T> {
    map<U>(f: (x: T) => U): Observable<U>;
  }
} 
// 擴(kuò)展聲明
Observable.prototype.map = function (f) {
  // ... another exercise for the reader
};

// consumer.ts
import { Observable } from "./observable";
import "./map";
let o: Observable<number>;
o.map((x) => x.toFixed());

全局?jǐn)U展

如果在模塊中,也可以在全局聲明中來(lái)擴(kuò)展:

// observable.ts
export class Observable<T> {
  // ... still no implementation ...
}
// 在這里擴(kuò)展
declare global {
  interface Array<T> {
    toObservable(): Observable<T>;
  }
}
Array.prototype.toObservable = function () {
  // ...
};

到此這篇關(guān)于typescript中聲明合并介紹的文章就介紹到這了,更多相關(guān)typescript聲明合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

龙海市| 邻水| 赣州市| 抚顺市| 贵港市| 蒲城县| 沽源县| 安化县| 桃江县| 舟山市| 吴忠市| 晋中市| 唐海县| 深州市| 株洲市| 通城县| 商河县| 余干县| 石狮市| 且末县| 通辽市| 揭阳市| 光山县| 巴彦县| 犍为县| 朝阳区| 塘沽区| 昆山市| 博爱县| 全南县| 富川| 福海县| 崇礼县| 绥棱县| 株洲县| 巍山| 高州市| 长治县| 古丈县| 荣昌县| 瓮安县|