Typescript: Extending Set In Class Causes Error (constructor Set Requires 'new')
I'm trying to implement some basic operations to the Set object like says here This is the code export class Conjunto extends Set{ constructor(initialValues?) { su
Solution 1:
if you are trying to add functions to the Set prototype, or add polyfills to Set, you can do the following:
declareglobal {
interfaceSet<T> {
// polyfillisSuperset(subset: Set<T>) : boolean;
// new functionsomeNewFunc(): boolean;
}
}
// add polyfill to the Set prototype as mentioned in the doc you provided: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Set Set.prototype.isSuperset = function(subset) {
for (var elem of subset) {
if (!this.has(elem)) {
returnfalse;
}
}
returntrue;
}
//add the new someNewFunc;Set.prototype.someNewFunc = function() {
// some logic here...returntrue;
}
to use:
stringSet =newSet<string>()
stringSet.isSuperset(someOtherSet);
stringSet.someNewFunc(); //returnstrue
Post a Comment for "Typescript: Extending Set In Class Causes Error (constructor Set Requires 'new')"