Here's what I have, I have various types, and I have a string representation for that type:
type PossibleValueTypes = Color | number | Position;
type PossibleValueTypesAsString = "color" | "number" | "position";
type ValueTypeMap = {
"color" : Color;
"number" : number;
"position" : Position;
};
Now, give a given string representation, returning the the value type is easy enough, for example;
function generateRandomValueOfType<T extends PossibleValueTypesAsString>(type: T) : ValueTypeMap[T] {
if (type === 'color') {
return {
r: 1,
g: 1,
b: 1,
a: 1,
} as ValueTypeMap[T]
}
if (type === "position") {
return {
x:1,
y: 1
} as ValueTypeMap[T]
}
if (type === "number") {
return 1 as ValueTypeMap[T];
}
throw new Error ("We shouldn't have got here");
};
const a: Position = generateRandomValueOfType("position");
const b: number = generateRandomValueOfType("position"); // Expected error!
Now the question is - what if I want to do the opposite, that is - given a value type, determine the string representation.
The best I've got is to do a chained conditional like:
type ReverseValueTypeLookup<T extends PossibleValueTypes> = T extends Color ? "color" : T extends number ? "number" : T extends Position ? "position": never;
function hasAValueAcceptingCallback<T extends PossibleValueTypes> (callback: (value: T) => void, valueTypeAsString: ReverseValueTypeLookup<T>) {
const value = generateRandomValueOfType(valueTypeAsString);
//Argument of type 'ValueTypeMap[ReverseValueTypeLookup<T>]' is not assignable to parameter of type 'T'.
//'ValueTypeMap[ReverseValueTypeLookup<T>]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'PossibleValueTypes'.
callback(value);
}
But this doesn't actually work in the example I've given.
Is there a tidier way to achieve both of the thing I'm trying to do here?