[英]Factory function in a Typescript declares file, with and without the new keyword
以下代碼將在ES5中創建工廠函數:
function MyClass(val) {
if (!(this instanceof MyClass)) {
return new MyClass(val);
}
this.val = val;
}
可以使用或不使用new
關鍵字來調用此函數:
var a = new MyClass(5);
var b = MyClass(5);
這在Typescript中可以正常工作,但是我無法弄清楚如何通過合並來描述兩個行為的聲明文件。 有沒有辦法做到這一點?
interface MyClass {
val: {};
}
interface MyClassConstructor {
(val: {}): MyClass;
new (val: {}): MyClass;
}
declare const MyClass: MyClassConstructor;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.