I'm trying to create an object where each property is mapped to a class instance. The class type relies on two generics. When I access a property in my object, TypeScript infers the type of the property as the union type used to define the object rather than the type of the property itself.
How can I get TypeScript to infer the type of the property properly?
Here is a code snippet to help clarify the problem:
export class CacheWithExpiry<T extends CacheTypes, V extends CacheNames> {
constructor() {
}
...
}
export type CacheNames =
'foo_1' |
'foo_2'
type CacheTypes = string | number
type CacheMap<T extends CacheTypes, V extends CacheNames> = { [key: string]: CacheWithExpiry<T, V> }
export const cacheMap: CacheMap<CacheTypes, CacheNames> = {
foo1Cache: new CacheWithExpiry<string, 'foo_1'>(), // Type is inferred as expected 'CacheWithExpiry<string, "foo_1">'
foo2Cache: new CacheWithExpiry<number, 'foo_2'>()
} as const
cacheMap.foo1Cache// Type is inferred as 'CacheWithExpiry<CacheTypes, CacheNames>' instead of 'CacheWithExpiry<string, "foo_1">'
Edit 1: I also need to maintain type safety for cacheMap so removing the type completely from cacheMap wouldn't work. Example:
export const cacheMap = {
foo1Cache: new CacheWithExpiry<string, 'foo_1'>(),
foo2Cache: new CacheWithExpiry<number, 'foo_2'>(),
foo3: 'I am not a cache class instance', // I still want this to throw an error
} as const