I'm trying to find the correct way to type a logical Dictionary object in TypeScript. The Record and Partial types combined do not work the way I want them to -- my "want" is to behave like a common dictionary/map available in other languages.
Given a dict: Dictionary<K,V> I want:
dict.keyto have the typeundefined|Vsince the key may not existObject.values(dict)to have the typeIterable<V>Object.entries(dict)to have the typeIterable<[K, V]>Object.keys(dict)to have the typeIterable<K>
Record<K,V> does not work, since accessing by key always yields the value type V, even though the key may not exist.
Partial<Record<K,V>> does not work, since then Object.values returns a type of Iterable<V|undefined>
How can I properly define a Dictionary type that works with standard JS objects?
It's fine if I have to override definitions of Object.values/entries/keys for a Dictionary type. I'm not clear on how I override Object functions for specific types only.
Note: I'm aware of a lot of the history of these types here. I'm interested in finding a solution that works. The above Dictionary is a common typing paradigm available in many languages, so I would like it to be possible in TypeScript.