title: Typescript interface property to string
body: <h2>Question/Answer - Update 2021</h2>
  <p><em>This questions was asked <strong>6 years ago</strong>, and I had very little understanding of Typescript!
  I don't want to remove it because there are still some people reading this post.</em></p>
  <p>If you want the type of a variable to be a property of another one, you can use <code>keyof</code>.</p>
  <p>Example:</p>
  <pre><code>interface User {
      name: string;
      age: number;
  }
  
  const nameProperty: keyof User = 'name'; // ok
  const ageProperty: keyof User = 'age'; // ok
  const emailProperty: keyof User = 'email'; // not ok
  </code></pre>
  <p>If you want a method that takes a parameter which is a property of another parameter you can use generics to link both types together.</p>
  <p>Example using generics + <code>keyof</code>:</p>
  <pre><code>const foo = &lt;TObject extends object&gt;(
      object: TObject,
      property: keyof TObject
  ) =&gt; {
      // You can use object[property] here
  };
  
  foo({ a: 1, b: 2 }, 'a'); // ok
  foo({ a: 1, b: 2 }, 'b'); // ok
  foo({ a: 1, b: 2 }, 'c'); // not ok
  </code></pre>
  <p>Example using generics + <code>Record</code>:</p>
  <pre><code>const foo = &lt;TKey extends string&gt;(
      object: Record&lt;TKey, unknown&gt;,
      property: TKey
  ) =&gt; {
      // You can use object[property] here
  };
  
  foo({ a: 1, b: 2 }, 'a'); // ok
  foo({ a: 1, b: 2 }, 'b'); // ok
  foo({ a: 1, b: 2 }, 'c'); // not ok
  </code></pre>
  <p><strong>Don't use this question answers please!</strong> <em>Typescript will automatically tell you that there is an error if you rename the property at some point.</em></p>
  <hr />
  <h2>Original question (2014)</h2>
  <p><strong>Objective</strong></p>
  <p>I have an interface TypeScript :</p>
  <pre><code>interface IInterface{
      id: number;
      name: string;
  }
  </code></pre>
  <p>I have some methods which take in entry the name of a property (string).</p>
  <p><strong>Ex</strong> :</p>
  <pre><code>var methodX = ( property: string, object: any ) =&gt; {
      // use object[property]
  };
  </code></pre>
  <p>My problem is that when i call <code>methodX</code>, I have to write the property name in string.</p>
  <p><strong>Ex :</strong> <code>methodX(&quot;name&quot;, objectX);</code> <em>where objectX implements IInterface</em></p>
  <p>But this is <strong>BAD</strong> : If i rename a property (let's say i want to rename <code>name</code> to <code>lastname</code>) i will have to update manually all my code.</p>
  <p>And I don't want this dependency.</p>
  <p>As typescript interfaces have no JS implementations, I don't see how I could not use string.</p>
  <p>I want to have something like : <code>methodX(IInterface.name.propertytoString(), objectX);</code></p>
  <p>I'm pretty new to JS, do you see an alternative ?</p>
  <p><strong>(Optional) More details :</strong> Why do I need to pass properties as parameter, and why I don't use a generic method ?</p>
  <p>I use methods that link data :</p>
  <pre><code>linkData = &lt;TA, TB&gt;(
      inputList: TA[],
      inputId: string,
      inputPlace: string,
      outputList: TB[],
      outputId: string ) =&gt; {
  
      var mapDestinationItemId: any = {};
      var i: number;
      for ( i = 0; i &lt; outputList.length; ++i ) {
          mapDestinationItemId[outputList[i][outputId]] = outputList[i];
      }
  
      var itemDestination, itemSource;
      for ( i = 0; i &lt; inputList.length; ++i ) {
          itemDestination = inputList[i];
          itemSource = mapDestinationItemId[itemDestination[inputId]];
          if ( itemSource ) {
              itemDestination[inputPlace] = itemSource;
          }
      }
  };
  </code></pre>
  <p>But TA and TB can have a lot of different ids. So i don't see how to make it more generic.</p>