export function getFromMapOrThrow(map: Map | WeakMap, key: K): V { const val = map.get(key); if (val === undefined) { throw new Error('missing value from map ' + key); } return val; } export function getFromMapOrCreate( map: Map | WeakMap, index: MapIndex, creator: () => MapValue, ifWasThere?: (value: MapValue) => void ): MapValue { let value = map.get(index); if (value === undefined) { value = creator(); map.set(index, value); } else if (ifWasThere) { ifWasThere(value); } return value; }