In my attempt to construct a specialized Map-like class that maps keys of one type to keys of another, I encountered a challenge. A straightforward approach would be to create a Map<keyof A, keyof B>
, but this method does not verify if the member types of each key align in both A
and B
.
I have initiated the implementation of the following class and contemplated enforcing type assertion to ensure that T
is not never
.
class KeyMap<A, B> {
private mapping = new Map<keyof A, keyof B>();
add<KA extends keyof A, KB extends keyof B, T = A[KA] & B[KB]>(
a: KA,
b: KB
): void {
this.mapping.set(a, b);
}
}
Although uncertain of its feasibility, I believe such mapping functionality could prove beneficial.
My overarching goal is to develop a versatile tool capable of seamlessly converting between A
and B
while ensuring type safety by validating all member type matches. If there exists a more effective approach to achieve this, I am open to alternative suggestions.