3

I want to declare a Map<T, K> type and the keys in the map is classes and values of the map should be instances of the corresponding key. For example:

class X {}

type InstanceMap = // the type that I want to declare

const map: InstanceMap = new Map();
map.set( X, new X() );
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
LCB
  • 971
  • 9
  • 22

1 Answers1

2

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375