The following leads to an error with this.#map
:
type MapType<K, V> = K extends object ? (Map<K, V> | WeakMap<K,V>) : Map<K,V>
export function getOrSet<K, V> (map: MapType<K, V>, key: K, create: (k: K) => V): V {
return create(key)
}
export class Select<T extends object> {
#map = new WeakMap<T, any>()
observable (item: T) { return getOrSet(this.#map, item, () => 'any') }
}
But this is incorrect because T extends object
and the error goes away when we change it to WeakMap<object, any>
.
Seems like a TS bug or I’m missing something.
Is there any fix/workaround here?
Reproduction on TS Playground
1