Subclassing Map with mixins

The problem arises because Map is defined with generics.

TS playground

Given a mixins extension

This extension just adds a creation_date attribute to any instance.

type Constructor = new (...args: any[]) => {}
function extension<TBase extends Constructor>(Base: TBase) {
  return class extends Base {
    creation_date = Date.now()
  }
}
class Foo extends extension(class {}) {}
console.log(new Foo().creation_date)

Extending Map

What we would like:

...
const mn = new XMap<string, number>()
console.log(mn.creation_date)
mn.set('foo', 123)
console.log(mn.get('foo'))

const ms = new XMap<string, string>()
console.log(ms.creation_date)
ms.set('foo', 'bar')
console.log(ms.get('foo'))

class Foo<K, V> {
  map?: Map<K, V>
}

Actually, we have

function mapExtension<K, V>() {
  return extension(class extends Map<K, V>{})
}

const MapNumber = mapExtension<string, number>()
const mn = new MapNumber()
console.log(mn.creation_date)
mn.set('foo', 123)
console.log(mn.get('foo'))

const MapString = mapExtension<string, string>()
const ms = new MapString()
console.log(ms.creation_date)
ms.set('foo', 'bar')
console.log(ms.get('foo'))

This is too wordy, although tolerable. But the most important thing is that we lost the Map generics.