TypeScript学习笔记-声明合并
生活随笔
收集整理的這篇文章主要介紹了
TypeScript学习笔记-声明合并
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
類不能和其他的類或者變量合并
/*** 聲明合并* 若兩個接口中存在相同的參數,那么這些相同的參數必須有相同的類型* 若兩個接口中存在相同的函數,那么同名的函數聲明都會被當成這個函數的重載,且后面的接口有更高的優(yōu)先級*/ interface Box {height: number;width: number;clone(animal: Sheep): Sheep; }interface Box {scale: number;clone(animal: Dog): Dog; }let animal: Sheep; let box: Box = { width: 20, height: 20, scale: 30, clone(animal) {} }; /*** 命名空間和類的合并* *********** 這讓我們可以表示內部類* *********** 合并后的命名空間只能互相訪問已經導出的成員*/class Album {label: Album.AlbumLabel; }namespace Album {export class AlbumLabel {} }//創(chuàng)建一個函數后,增加它的屬性,函數和命名空間的合并function buildLabel(name: string): string {return buildLabel.prefix + name + buildLabel.suffix; }namespace buildLabel {export let prefix = "Hello";export let suffix = ""; }buildLabel("zhangsan");//枚舉和命名空間的合并 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;}} }?
轉載于:https://www.cnblogs.com/goOtter/p/9773204.html
總結
以上是生活随笔為你收集整理的TypeScript学习笔记-声明合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习HTML5 canvas遇到的问题
- 下一篇: POJ - 3126 - Prime P