![](/img/trans.png)
[英]What is the use-case of enabling declaration and declarationMap in tsconfig.json?
[英]What is the use case for declaration merging?
我在閱讀有關TypeScript中聲明合並的內容 ,並且很難理解它的用例,尤其是對於接口。
在他們的文檔中,他們有以下示例:
也就是說,在示例中:
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; }
為什么要創建三個單獨的接口而不是結果聲明?
例如,您可能想向window對象添加一個方法,因此您可以這樣做:
interface Window {
myMethod(): string;
}
window.myMethod = function() {
...
}
當您需要填充時,這非常有用:
interface String {
trimLeft(): string;
trimRight(): string;
}
if (!String.prototype.trimLeft) {
String.prototype.trimLeft = function() { ... }
}
if (!String.prototype.trimRight) {
String.prototype.trimRight = function() { ... }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.