I don't think this is possible, it would require existentially quantified generic types. You can do
type InstanceMap<T> = Map<{new (): T}, T>
using a constructor type for the key, but this would only allow you to store a specific class (and maybe subclasses?) but not any class. Your code would compile with const map: InstanceMap<X> = new Map();
, but map.set(Y, new Y())
would fail.
Instead of trying to instantiate a Map
type, we could try to come up with our own interface where the existential quantifier is delegated to the individual methods:
interface InstanceMap {
set<T>(c: new () => T, i: T): void;
get<T>(c: new () => T): T | undefined;
}
This works for set
and get
, but breaks down as soon as you try to declare forEach
or iterators.