How can I get a map for type A at runtime ? The following code fails to compile with this error.
Argument of type 'A' is not assignable to parameter of type '"A" | "B" | "C"'. Type 'A' is not assignable to type '"C"'
type A = {
a: string;
};
type B = {
b: string;
};
type C = {
c: string;
};
const maps = {
A: (A) => {},
B: (B) => {},
C: (C) => {},
};
function getMap(source: keyof typeof maps) {
return maps[source];
}
const source: A = {
a: 'test'
}
getMap(source);
getMaptakes a type of'A' | 'B' | 'C'as an argument, notA | B | C, so to make it work you need to call it asgetMap('A'), but it's probably not what you wanted, because it requires the type name, not the type itself.