I'm struggling to write code for registering strongly typed data loaders. I'm specifically facing issues with TypeScript in setting the map correctly. In the scenario below, M
represents the service map and k
is a list of services with a field type that has a fixed value of E
. However, when attempting to assign an instance to the map, I encounter an error indicating that I am trying to assign to undefined. I'm unsure about how to proceed from this point.
enum E {
A = 'a',
B = 'b',
}
interface I<T extends E> {
type: T;
}
type J<T> = T extends E ? I<T> : never;
export type K = J<E>;
const M: { [T in E]?: I<T> } = {};
const k: K[] = [];
k.forEach(
<T extends E>(i: I<T>) => {
M[i.type] = i;
// ERROR
// Type 'I<T>' is not assignable to type '{ a?: I<E.A> | undefined; b?: I<E.B> | undefined; }[T]'.
// Type 'I<T>' is not assignable to type 'undefined'.
});