亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫(xiě)法(jQuery和class)

    JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫(xiě)法(jQuery和class)

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)選項(xiàng)卡插件的兩種寫(xiě)法:jQuery和class,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • js隱式轉(zhuǎn)換的知識(shí)實(shí)例講解

    js隱式轉(zhuǎn)換的知識(shí)實(shí)例講解

    在本篇文章中,小編給大家分享了關(guān)于js隱式轉(zhuǎn)換的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。
    2018-09-09
  • javascript addLoadEvent函數(shù)說(shuō)明

    javascript addLoadEvent函數(shù)說(shuō)明

    網(wǎng)頁(yè)加載完整后會(huì)觸發(fā)一個(gè)onload事件,默認(rèn)地一個(gè)事件只能和一個(gè)函數(shù)綁定。
    2010-01-01
  • escape、encodeURI 和 encodeURIComponent 的區(qū)別

    escape、encodeURI 和 encodeURIComponent 的區(qū)別

    escape(), encodeURI()和encodeURIComponent()是在Javascript中用于編碼字符串的三個(gè)常用的方法,而他們之間的異同卻困擾了很多的Javascript初學(xué)者,今天我就在這里對(duì)這三個(gè)方法詳細(xì)地分析與比較一下。
    2009-03-03
  • 全面解析Javascript無(wú)限添加QQ好友原理

    全面解析Javascript無(wú)限添加QQ好友原理

    這篇文章主要介紹了全面解析Javascript無(wú)限添加QQ好友原理的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • webpack4的遷移的使用方法

    webpack4的遷移的使用方法

    本篇文章主要介紹了webpack4的遷移的使用方法,主要介紹了如何從webpack1.x升級(jí)到4.x,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 詳解datagrid使用方法(重要)

    詳解datagrid使用方法(重要)

    這篇文章主要介紹了datagrid使用方法(重要),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 最新評(píng)論