I have an enum of possible values for the first parameter and I want the second parameter to be based on the first parameter. So, if NAME is given I want the second parameter to be as string. If AGE is given, I want the second parameter to be a number.
How can I do something like this?
enum KeyType {
NAME,
AGE
}
class MyClass {
public static setProperty(key: KeyType.NAME, value: string): void { }
public static setProperty(key: KeyType.AGE, value: number): void { }
}
And I would want to call the method like this:
MyClass.setProperty(KeyType.NAME, 'John');
Also, this should show an error:
MyClass.setProperty(KeyType.NAME, 5); // 5 is not a string
In this example it doesn't work as the key type is wrongly defined (key type is actually the value of the enum, so key type is 0).
I am also open to suggestions about a different approach to having this functionality of only allowing a specific type for a specific parameter key.