Skip to content
Advertisement

Syntax for extends or implements a plain object

I have this:

export class DESSet<V> extends Set<V extends {id: string}> {

  toJSON() {

    return {
      size: this.size,
      values: Array.from(this.values()).map(v => v.id)
    }

  }
}

this syntax doesn’t seem to work. I also tried:

Set<V implements {id: string}>

but that’s no bueno either, do I really need to use a class or interface, is there not a way to do it inline?

Advertisement

Answer

Whoops that was dumb, just put the extends on the wrong generic parameter, this is fine:

export class DESSet<V extends {id:string}> extends Set<V> {
  // ...
}

and so is this:

export interface HasId {
  id: string
}


export class DESSet<V extends HasId> extends Set<V> {
  // ...
}
Advertisement