If my union type is structured like this:
type StateUpdate = { key: 'surname', value: string }
| { key : 'age', value: number };
This setup is convenient because it allows me to determine the type of the value based on the key.
However, I am looking for a way to create functions that can only be called with valid keys from the union type. In this case, it would have to be either 'surname' or 'age'.
I could define another type as follows:
type ValidKey = 'surname' | 'age';
Then I could define a function like:
(keyName: ValidKey) => (console.log(keyName)); // ...or any logic here
This approach requires manual coordination between the two types. Is there an automatic way to handle this, or perhaps a more efficient alternative?