我想在打字稿中扩展本机javascript类型。使用接口来声明扩展属性是可能的。但是如何声明重载属性?
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"){
}
}
}
谢谢
你近了。
interface HTMLElement {
add(a:string): void;
add(a:boolean): void;
}
提示:我总是在lib.d.ts文件中查看Microsoft的实现。在这种情况下,我键入(使用Visual Studio代码):document.addEventListener并查看(使用ctrl +左键单击)Microsoft如何创建界面。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句