How to express a type that represents an associative array whose keys determine the type of the value? I'm fairly new to type systems and theory, so I would appreciate some guidance in a problem that sparked my interest.
I would like to understand what type system features are required so a compiler can enforce that a given key will return a value of the same type as the one the key was associated with in the first place.
A practical version of my problem is to declare a Map in TypeScript that allows a developer experience like in the pseudocode below:
const cache =  new  Map&lt;K,  V&gt;()

cache.set('Foo',  Error('R'))

cache.set('Bar',  1)

cache.get('Foo')  // Return value typed as Error.
  
cache.get('Bar')  // Return value typed as number.

cache.get('Qux')  // Compilation error.

What would the type of K and V be?
What happens if I try to get Bar before it has been set? If you expect a compilation error, then you are not talking about a single type, but instead a sequence of types, where the type changes after each call to the set method. Look into 'strong update' and 'linear types'.
Why not just use a record type?
type MyRecord = { Foo : Error, Bar : number }