title: TypeScript Generic method signature: Adding inline constraints, so that a value is both - in object keynames and a string
body: <p>How to alter the signature (inline) of <code>sortArrayItemsByDate</code>, so as to introduce the following additional constraints (cumulatively):</p>
  
  <ol>
  <li><code>T</code> should have a key named <code>propName</code> (a value is in object key-names)</li>
  <li><code>T[propName]</code> should be a string (in the same time)</li>
  </ol>
  
  <p>Link to StackBlitz <a href="https://stackblitz.com/edit/so-zaggi-typescript-generic-signature-with-return-type" rel="nofollow noreferrer">playground</a></p>
  
  <pre><code> const sortArrayItemsByDate = &lt;T&gt;(items: T[], propName: string): T[] =&gt;
      items.sort((a: T, b: T) =&gt; Date.parse(a[propName]) - Date.parse(b[propName]));
  
  
   interface StudentModel {
      name: string;
      birthday: string;
   }
  
    const s0: StudentModel = {
      name: "Peter",
      birthday: "2001-11-23",
    };
  
    const s1: StudentModel = {
       name: "John",
       birthday: "2003-11-30",
    };
  
    const students: StudentModel[] = [s0, s1];
    const studentsSorted = sortArrayItemsByDate(students, 'birthday');
  </code></pre>