79371239

Date: 2025-01-20 12:06:57
Score: 1.5
Natty:
Report link

Trying to solve the problem with possible to pass not all needed keys for create class instance, mentioned by @jcals, I think I need custom class which constructor takes only object of corresponds keys, value type:

function getEntries<
  T extends Record<PropertyKey, unknown>,
  K extends keyof T,
  V extends T[K]>(o: T) {
    return Object.entries(o) as [K, V][]
}

interface T_Dict<K extends PropertyKey, V> extends ReadonlyMap<K, V> {get(key: K): V}

export class DictObject<K extends PropertyKey, V> extends Map<K, V> implements T_Dict<K, V>{
  constructor(obj: Record<K, V>) {
    super(getEntries(obj))
  }

  public override get(key: K): V {
    if (!this.has(key)) throw new Error('Wrong key!')
    return this.get(key)
  }
}

But I cant force it to produce instanse of ReaadOnlyMap - it's still has method like delete/clear (

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @jcals
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Poul