2016-05-20 59 views
0

如何重载函数我想在typescript中扩展原生的javascript类型。这可以使用一个接口来声明扩展属性。 但如何声明重载的属​​性?Typescript:接口

interface HTMLElement { 
    add:(a:string)=>void; // error: add is duplicate 
    add:(a:boolean)=>void; 
} 

HTMLElement.prototype.add = function (a):void{ 
    if(typeof a=="string"){ 

    } 
    else if(typeof a=="boolean"){ 

    } 
} 

class HTMLElement2 { 
    add(a:string):void; // ok 
    add(a:boolean):void; 
    add(a):void{ 
     if(typeof a=="string"){ 

     } 
     else if(typeof a=="boolean"){ 

     } 
    } 
} 

谢谢

回答

1

你接近。

interface HTMLElement { 
    add(a:string): void; 
    add(a:boolean): void; 
} 

提示:我总是看着微软在lib.d.ts文件中的实现。在这种情况下,我键入(使用Visual Studio代码):document.addEventListener并查看(使用Ctrl +左键单击)Microsoft如何创建界面。

+0

我不知道为什么我没有尝试过。很明显。 – Baud