2016-11-04 69 views
0
class ExtendSet<T> extends Set<any> { 
    constructor(value: any) { 
    super(value); 
    } 
} 

var s = new ExtendSet([1, 2, 3, 4, 5]); 

console.log(s); 

類型錯誤:構造函數中設置需要「新」如何從本地構造打字稿延長

其實,我只是想延長集對象的一些方法,如指定者():任何[]

{ 
    "compilerOptions": { 
    "module": "commonjs", 
    "target": "es5", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "types": [ 
     "mocha", 
     "node", 
     "typescript", 
     "core-js" 
    ] 
    }, 
    "exclude": [ 
    "node_modules" 
    ] 
} 

回答

0

我不知道爲什麼你得到這個錯誤。如果你去this操場片段,你會發現它沒有錯誤。也許你正在使用typcript的過時版本。

至於你正在努力實現的東西:

我只是想延長集對象的一些方法,如指定者():任何[]

更簡單的方法是做如下:

interface Set<T> { 
    toArray:() => any 
} 

Set.prototype.toArray =() => { 
    // write your code here 
} 
+0

我想從Set類擴展一個新的類,而不是寫在原始構造函數 – axetroy