I am attempting to dynamically assign the output of a function based on the name of the input parameter within an existing interface:
interface MyInterface {
typeA: string;
typeB: boolean;
typeC: string;
typeD: number;
...
}
const myFunction: (parameterName: string) => MyInterface[parameterName] =
(parameterName) =>
{
...
return valueXYZ
}
An error is appearing that states:
'parameterName' refers to a value, but is being used as a type here.
Currently, the variables defined in this manner must each implement their specific type individually, preventing me from typing my function in a more general way as envisioned above.
const valueA: MyInterface['typeA'] = myFunction('typeA');
const valueB: MyInterface['typeB'] = myFunction('typeB');
const valueC: MyInterface['typeC'] = myFunction('typeC');
const valueD: MyInterface['typeD'] = myFunction('typeD');
...
Is there a method to instruct Typescript to utilize the parameterName as the propertyName defined in the interface?