打字稿:在 class 中扩展集会导致错误(构造函数集需要 'new')

Typescript: Extending Set in class causes error (Constructor Set requires 'new')

我正在尝试对 Set 对象实施一些基本操作,如 here

这是代码

export class Conjunto extends Set<any>{
  constructor(initialValues?) {
    super();
    return new Conjunto(initialValues);
  }

  isSuperset(subset) {
    //some code
  }
}

你们有什么想法让它发挥作用吗?还是我做错了什么?

目前我正在使用这个人发现的 hack here

如果您尝试向 Set 原型添加函数,或向 Set 添加 polyfill,您可以执行以下操作:

declare global {
  interface Set<T> {
      // polyfill
      isSuperset(subset: Set<T>) : boolean;
      // new function
      someNewFunc(): 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)) {
            return false;
        }
    }
    return true;
}

//add the new someNewFunc;
Set.prototype.someNewFunc = function() {
    // some logic here...
    return true;
}

使用:

stringSet = new Set<string>()
stringSet.isSuperset(someOtherSet);
stringSet.someNewFunc(); // returns true