goal
I have a TypeScript interface:
interface IInterface{ id: number; name: string; }
I have several methods that take the name of the property (string).
Ex :
var methodX = ( property: string, object: any ) => {
My problem is that when I call methodX , I have to write the name of the property in a string.
Example: methodX("name", objectX); where objectX implements IInterface
But this is bad . If I rename a property (say, I want to rename name to lastname ), I will have to manually update all my code.
And I do not want this addiction.
Since TypeScript interfaces do not have JS implementations, I cannot see how I could not use the string.
I want to have something like: methodX(IInterface.name.propertytoString(), objectX);
I'm new to JS, do you see an alternative?
(optional) Additional information: Why do I need to pass properties as a parameter and why am I not using a generic method?
I use methods that bind data:
linkData = <TA, TB>( inputList: TA[], inputId: string, inputPlace: string, outputList: TB[], outputId: string ) => { var mapDestinationItemId: any = {}; var i: number; for ( i = 0; i < outputList.length; ++i ) { mapDestinationItemId[outputList[i][outputId]] = outputList[i]; } var itemDestination, itemSource; for ( i = 0; i < inputList.length; ++i ) { itemDestination = inputList[i]; itemSource = mapDestinationItemId[itemDestination[inputId]]; if ( itemSource ) { itemDestination[inputPlace] = itemSource; } } };
But TA and TB can have many different identifiers. Therefore, I do not see how to make it more general.