如何从本地构造打字稿延长
问题描述:
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"
]
}
答
我不知道为什么你得到这个错误。如果你去this操场片段,你会发现它没有错误。也许你正在使用typcript的过时版本。
至于你正在努力实现的东西:
我只是想延长集对象的一些方法,如指定者():任何[]
更简单的方法是做如下:
interface Set<T> {
toArray:() => any
}
Set.prototype.toArray =() => {
// write your code here
}
我想从Set类扩展一个新的类,而不是写在原始构造函数 – axetroy